Android button设置背景色与边框颜色

                                                                         Android button设置背景色与边框颜色

1.在drawable文件夹下面创建setbar_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 背景色 -->
    <solid android:color="#00000000"/>
    <!-- 边框色 -->
    <stroke android:width="0.5dip" android:color="#f00" />
</shape>
2.设置button顺序

 android:background="@drawable/setbar_bg"


去看看效果吧!

  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 在Android中,可以通过设置Button背景颜色来改变其外观。可以使用XML文件或Java代码来设置Button背景颜色。使用XML文件时,可以在Button的属性中设置背景颜色,例如: ``` <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" android:background="#FF000" /> ``` 这里的android:background属性设置Button背景颜色为红。如果使用Java代码来设置Button背景颜色,可以使用setBackground()方法,例如: ``` Button button = findViewById(R.id.button); button.setBackground(getResources().getDrawable(R.drawable.button_bg)); ``` 这里的button_bg是一个自定义的背景图片,可以在drawable文件夹中创建。这个背景图片可以是一个纯的矩形,也可以是一个渐变的矩形,或者是一个带有边框和阴影的矩形。 ### 回答2: Android中的Button控件是常用的用户交互组件之一,而控件的背景颜色一直是影响用户界面体验的重要因素之一。在Android中,Button背景颜色可以通过以下方式进行设置: 1. XML布局文件中设置背景颜色 可以在Button的XML布局文件中设置background属性来设置Button背景颜色,例如: ``` <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" android:background="@color/my_color" /> ``` 其中,@color/my_color是指在colors.xml文件中定义的颜色值。可以使用颜色名称、十六进制颜色值或RGB值等方式定义颜色值。例如,在colors.xml文件中定义一个颜色值为#FF0000的红: ``` <color name="red">#FF0000</color> ``` 然后,在Button的布局文件中使用该颜色值: ``` <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" android:background="@color/red" /> ``` 2. Java代码中设置背景颜色 也可以通过Java代码来设置Button背景颜色,例如: ``` Button myButton = findViewById(R.id.my_button); myButton.setBackgroundColor(Color.RED); ``` 其中,Color.RED是指Android系统定义的颜色值之一,也可以使用其他颜色值来替换。 同时,还可以通过设置drawable来设置Button背景颜色。具体实现方式可以参考Android开发文档。 ### 回答3: 在Android中,我们可以使用布局文件或程序代码来设置按钮(Button)的背景颜色。下面将分别介绍: 1. 布局文件中设置背景颜色 我们可以在布局文件的Button标签中,使用android:background属性来设置按钮的背景颜色。具体的做法是,在布局文件中添加Button控件,如下所示: ``` <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是一个按钮" android:background="#ff0000" /> ``` 在上面的代码中,我们给Button控件设置了id、宽、高、文本和背景颜色。其中,android:background属性的值为#ff0000,表示按钮的背景颜色为红。 2. 程序代码中设置背景颜色 我们也可以在程序代码中,使用setBackgroundResource()或setBackgroundColor()方法来设置按钮的背景颜色。具体的做法如下: (1)使用setBackgroundResource()方法 ``` Button myButton = findViewById(R.id.my_button); myButton.setBackgroundResource(R.color.red); ``` 在上面的代码中,我们先获取了Button控件的实例,然后使用setBackgroundResource()方法,将按钮的背景颜色设置为red.xml(在res/values/colors.xml中定义)。 (2)使用setBackgroundColor()方法 ``` Button myButton = findViewById(R.id.my_button); myButton.setBackgroundColor(Color.RED); ``` 在上面的代码中,我们同样获取了Button控件的实例,然后使用setBackgroundColor()方法,将按钮的背景颜色直接设置为Color.RED(Android系统提供的颜色值之一)。 需要注意的是,使用java代码设置按钮背景颜色时,还可以使用Drawable对象、Bitmap对象等自定义的背景样式来设置背景。这里就不再详细讨论了。 总之,在Android中,设置按钮的背景颜色非常简单,可以根据自己的需求,选择使用布局文件或Java代码来设置即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值