一、AndroidButton属性
AndroidButton 是继承自Button的控件,所以Button的大部分属性 AndroidButton 都会继承,比如android:layout_width 和 android:layout_height,而 AndroidButton 独有并且比较重要的属性有:
1. app:cornerRadius
2. app:colorNormal
3. app:colorPressed
4. app:rippleColor
这些属性也是 AndroidButton 的特色之处,下面我们将会对它们进行详解。
二、AndroidButton按钮背景颜色
AndroidButton 允许你通过配置按钮背景颜色来使按钮更具特色。这些颜色可以由 app:colorNormal 和 app:colorPressed 来指定。colorNormal 是指未按下状态下的颜色,colorPressed 是指在按钮被按下时的背景颜色。
下面是一个例子:
<com.agrawalsuneet.squareloaderspack.loaders.WheelLoader
android:id="@+id/squareLoader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:colorNormal="#03A9F4"
app:colorPressed="#0288D1" />
例子指定了一个叫做 squareLoader 的 AndroidButton,它的 colorNormal 是 #03A9F4,colorPressed 是 #0288D1。
三、AndroidButton居中
要让 AndroidButton 水平居中可以通过在布局文件中设置 layout_gravity 属性实现:
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:layout_gravity="center_horizontal"/>
上述代码中的 layout_gravity 属性设置为 center_horizontal,它可以使 button 在水平方向上自动居中。
四、AndroidButton背景颜色
对于 AndroidButton,我们还可以通过 app:rippleColor 和 app:cornerRadius 来对按钮进行更多的样式自定义。
app:rippleColor 是指按钮点击时的背景颜色。app:cornerRadius 是指 AndroidButton 的圆角大小。下面是一个代码示例:
<com.agrawalsuneet.squareloaderspack.loaders.WheelLoader
android:id="@+id/squareLoader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:colorNormal="#03A9F4"
app:colorPressed="#0288D1"
app:cornerRadius="5dp"
app:rippleColor="#FF4081" />
上述代码中的cornerRadius属性设置为 5dp,它将会使 AndroidButton 的圆角大小为 5dp。rippleColor 属性设置为 #FF4081,它将会在按钮点击时弹出波纹的深粉色颜色。
五、AndroidButton圆角代码
app:cornerRadius 属性可以用来设置 AndroidButton 的圆角大小。我们可以把圆角大小设为大于 0 的任意值。下面是一个 AndroidButton 圆角代码的示例:
<com.agrawalsuneet.squareloaderspack.loaders.WheelLoader
android:id="@+id/squareLoader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:colorNormal="#03A9F4"
app:colorPressed="#0288D1"
app:cornerRadius="10dp"
app:rippleColor="#FF4081" />
上述代码中的 cornerRadius 属性设置为 10dp,它将会使 AndroidButton 的圆角大小为 10dp。
六、AndroidButton颜色怎么改
AndroidButton 的颜色可以使用 app:colorNormal 和 app:colorPressed 属性进行修改。颜色值为 16 进制颜色代码。
下面是一个代码示例:
<com.agrawalsuneet.squareloaderspack.loaders.WheelLoader
android:id="@+id/squareLoader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:colorNormal="#4CAF50"
app:colorPressed="#388E3C"
app:cornerRadius="5dp"
app:rippleColor="#FF4081" />
上述代码中的 colorNormal 属性设置为 #4CAF50,它将会使未按下状态下 AndroidButton 的背景色为绿色。colorPressed 属性设置为 #388E3C,它将会使按下状态下 AndroidButton 的背景色为绿色。
七、AndroidButton点击前后颜色
AndroidButton 点击前后颜色的代码实现和颜色的修改相似,只需要设置 app:colorNormal 和 app:colorPressed 属性即可。
下面是一个代码示例:
<com.agrawalsuneet.squareloaderspack.loaders.WheelLoader
android:id="@+id/squareLoader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:colorNormal="#F44336"
app:colorPressed="#E53935"
app:cornerRadius="5dp"
app:rippleColor="#FF4081" />
上述代码中的 colorNormal 属性设置为 #F44336,它将会使未按下状态下 AndroidButton 的背景色为红色。colorPressed 属性设置为 #E53935,它将会使按下状态下 AndroidButton 的背景色为红色。
八、AndroidButton如何将两个按钮并排
在 Android 中,我们可以使用LinearLayout 或者 RelativeLayout 实现并排布局。下面是一个代码示例:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="Button 1"/>
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="Button 2"/>
</LinearLayout>
上述代码中将两个按钮都放到了一个 LinearLayout 中,并将 LinearLayout 的 orientation 属性设置为 horizontal。当前布局将在水平方向上对其两个按钮,使它们位于同一行。
九、android按钮样式
AndroidButton 的样式可以使用 android:background 属性进行设置。下面是一个代码示例:
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Sign up"
android:background="@drawable/button_background_yellow"
android:textColor="@color/colorWhite"/>
上述代码中给AndroidButton设置了一个自定义背景图。并将 AndroidButton 的文字颜色设置为白色。可以根据自己的需求去调整这些属性,自定义 AndroidButton 的样式。
十、android studio按钮控件选取
Android Studio 是一款非常好用的 Android 开发工具,它内置了大量的工具和代码资源,包括各种控件,比如 AndroidButton。您可以通过在您的项目中使用控件库,直接从项目中拖动 AndroidButton 至您的布局文件中。
下面是一个代码示例:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="Button 1"/>
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="Button 2"/>
</LinearLayout>
上述代码中将两个按钮都放到了一个 LinearLayout 中,并将 LinearLayout 的 orientation 属性设置为 horizontal。当前布局将在水平方向上对其两个按钮,使它们位于同一行。
通过这样的操作,您可以轻松在 Android Studio 中添加 AndroidButton。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/181392.html
微信扫一扫
支付宝扫一扫