用自定义.9图设置为android button的背景

.9图是可以能够在宽度和高度上缩放的特别种类的图像。9图是定义android button外观最普通的一种方式。

注意:在边界周围的1个像素的黑色线控制这图片的缩放。

下面是被用在这个例子上面的图片。



  1. 保存这个图片到drawable文件夹。/res/drawable/ic_green_button.9.png
  2. 你可以像下面解释的那样定义一个新的样式。我们给创建的两个button创建了两个样式MyButton.Green和MyButton.Orange。

<style name="MyButton" parent="android:Widget.Button">
	<item name="android:layout_width">fill_parent</item>
	<item name="android:layout_height">wrap_content</item>
	<item name="android:textColor">#ffffff</item>
	<item name="android:textSize">22sp</item>
</style>
<style name="MyButton.Green">
	<item name="android:background">@drawable/ic_green_button</item>
</style>
<style name="MyButton.Orange">
	<item name="android:background">@drawable/ic_orange_button</item>
</style>

应用新的button style到你的button
<Button
style="@style/MyButton.Green"
android:text="GREEN BUTTON" />
<Button
style="@style/MyButton.Orange"
android:text="ORANGE BUTTON" />


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将Android Button的系统自定义背景替换为自己设置背景,可以按照以下步骤进行操作: 1. 在res/drawable目录下创建一个新的XML文件,例如button_background.xml。 2. 在button_background.xml中添加以下代码来设置自定义背景: ``` <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#FF4081"/> <corners android:radius="20dp"/> <stroke android:width="1dp" android:color="#FFFFFF"/> </shape> ``` 这个例子中设置背景是一个矩形,有圆角和边框,颜色为粉色。 3. 在布局文件中,为Button添加属性android:background,指向刚刚创建的XML文件,例如: ``` <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="My Button" android:background="@drawable/button_background" /> ``` 这样就完成了Button自定义背景替换。 注意:如果要在不同的状态下设置不同的背景,可以在XML文件中添加不同的item来指定不同的背景,例如: ``` <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/> <item android:drawable="@drawable/button_focused" android:state_focused="true"/> <item android:drawable="@drawable/button_normal"/> </selector> ``` 这个例子中,设置Button在不同状态下的背景,分别是按下状态、焦点状态和普通状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值