当前位置:首页 » 安卓系统 » androidbutton背景颜色

androidbutton背景颜色

发布时间: 2023-06-30 14:15:51

‘壹’ android怎么改变按钮颜色

可以用代码设置替换Button的背景颜色
btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.图片的路径));

‘贰’ android 中,我的按钮不会像默认按钮一样,长按后按钮背景会变橘黄了,是怎么回事如何弄回来呢

android中的按钮巧空默认一班都是浅灰色的,不是纯白的,点击时变成蔽宽绝橘黄色,是系统默认当按钮获取到焦点时的一个状态变化(可以理解为背景颜色的变化)通过修改属性xml这些都是可以改变的,我们做UI时都是美工做的图片,都不用系统默认的,太宏姿难看了。

‘叁’ 【Android】关于Button背景色/样式设置失效

在设置button背景颜色后,button背景仍然为蓝色。如下所示:

在使用Android Studio 4.1+ 进行开发时,创建的项目默认的主题是 Theme.MaterialComponents.DayNight.DarkActionBar
。所有Button都是Material类型的Button,默认使用主题色。

解决方法:

android - How to change the color of a button? - Stack Overflow

‘肆’ 安卓界面布局如何改变所有button的背景颜色

可以使用selector来实现Button的特效

main.xml

Xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="按下或者获得焦点Button会变不同颜色"
<SPAN style="COLOR: #ff0000">android:textColor="@color/button_text" </SPAN>/>
</LinearLayout>
www.2cto.com
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="按下或者获得焦点Button会变不同颜色"
android:textColor="@color/button_text" />
</LinearLayout>

XML 文件保存在res/color/button_text.xml

Xml代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true" android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>

背景选择器-selector

概述
在drawable/xxx.xml中配置,通过配置selector,可以使系统运行时根据控件对象的状态使用相应的图片、文字等。
selector中的常用属性
android:state_selected 控件选中状态,可以为true或false
android:state_focused 控件获得焦点状态,可以为true或false
android:state_pressed 控件点击状态,可以为true或false
android:state_enabled 控件使能状态,可以为true或false
android:state_checkable 控件可勾选状态,可以为true或false
android:state_checked 控件勾选状态,可以为true或false
注意:在状态描述中,第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,所以默认的值必须总是在最后。
android:window_focused 应用程序窗口焦点状态,可以为true或false
android:color 定义特定状态的颜色
#rgb
#argb
#rrggbb
#aarrggbb
为16进制颜色。这个颜色由rgb值指定,可带alpha,必须以”#“开头,后面跟随alpha-red-green-blue信息,格式可以为:
使用selector设置背景
把下面的XML保存成.xml文件(比如list_item_bg.xml),运行时系统会根据ListView中列表项的状态来使用相应的背景图片。
drawable/list_item_bg.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 默认时的背景图片 -->
<item android:drawable="@drawable/pic1" />

<!-- 没有焦点时的背景图片 -->
<item android:state_window_focused="false"
android:drawable="@drawable/pic1" />

<!-- 非触摸模式下获得焦点并单击时的背景图片 -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable= "@drawable/pic2" />

<!-- 触摸模式下单击时的背景图片 -->
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/pic3" />

<!--选中时的图片背景 -->
<item android:state_selected="true"
android:drawable="@drawable/pic4" />

<!--获得焦点时的图片背景 -->
<item android:state_focused="true"
android:drawable="@drawable/pic5" />
</selector>

使用方法
第一种是在listview中配置android:listSelector=”@drawable/list_item_bg”
第二种是在listview的item中添加属性android:background=”@drawable/list_item_bg”
第三种是java代码中使用:
Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg);
listview.setSelector(drawable);

注:列表有时候为黑的情况,需要加上下面的代码使其透明:
android:cacheColorHint="@android:color/transparent"

使用selector设置字体颜色
drawable/button_font.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#FF0000" />
<item android:state_focused="true" android:color="#00FF00" />
<item android:state_pressed="true" android:color="#0000FF" />
<item android:color="#000000" />
</selector>

使用方法
android:textColor="@drawable/button_color"

更复杂的效果
还可以实现更复杂的效果,例如渐变等等。 drawable/button_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<!-- 定义当button 处于pressed 状态时的形态。-->
<shape>
<gradient android:startColor="#8600ff" />
<stroke android:width="2dp"
android:color="#000000" />
<corners android:radius="5dp" />
<padding android:left="10dp"
android:top="10dp"
android:bottom="10dp"
android:right="10dp"/>
</shape>
</item>
<item android:state_focused="true">
<!-- 定义当button获得 focus时的形态 -->
<shape>
<gradient android:startColor="#eac100"/>
<stroke android:width="2dp"
android:color="#333333"
color="#ffffff"/>
<corners android:radius="8dp" />
<padding android:left="10dp"
android:top="10dp"
android:bottom="10dp"
android:right="10dp"/>
</shape>
</item>
</selector>

使用方法
android:background="@drawable/button_color"
android:focusable="true"

‘伍’ Android studio的问题,为什么button无论设置什么背景,都是蓝色的,没有一点变化

这个就看mipmap目录下的btn文件是啥颜色的了,如果没问题的话,再看看是不是有其他布局覆盖在Button上了。ps:你新建一个button 不设置任何背景属性,去排查一下是哪方面的问题

热点内容
安卓手机版的蒸汽平台在哪里下载 发布:2025-02-07 00:29:04 浏览:453
php自学中心 发布:2025-02-07 00:28:50 浏览:269
手机卡在哪里设置密码 发布:2025-02-07 00:21:25 浏览:706
解压哄睡音频 发布:2025-02-07 00:19:56 浏览:158
数据库系统第六版答案 发布:2025-02-07 00:06:10 浏览:970
fut加密毛发怎么样 发布:2025-02-07 00:06:01 浏览:868
java三目表达式 发布:2025-02-06 23:58:41 浏览:741
android开启wifi 发布:2025-02-06 23:50:08 浏览:496
腾讯云服务器是不是只有c盘 发布:2025-02-06 23:50:03 浏览:473
安卓如何选择相册 发布:2025-02-06 23:49:57 浏览:346