xml中,button改变背景颜色方法
在画几个设置界面,用到了button控件,对于button空间的背景色在不同状态下的颜色改变方法,做了一下尝试,发现了两种背景颜色改变的方法,就总结了下。
方法一尝试了好多遍才好,要点在于,在selector中android:drawable="@drawable/button_focus"引号中为xml文件,此xml文件为color类型,且在此color xml文件中
<color xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/button_focus_color"> <!-- 注意此处android:color的位置 -->
</color>
android:color="@color/button_focus_color"在color控件中。
方法一:填充button背景颜色的方法
在factory_reset这个xml文件中,其具体xml文件为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="560px"
android:layout_height="348px"
android:background="#212121"
android:orientation="vertical"
android:layout_gravity="center_vertical|center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <!-- 怎样设置 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|bottom"
android:text="确定要恢复出厂设置吗?"
android:textColor="#e6e6e6"
android:textSize="34px"
android:paddingTop="68px"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal" >
<Button
android:id="@+id/bn1"
android:layout_width="520px"
android:layout_height="72px"
android:text="保存"
android:textSize="28px"
android:gravity="center_vertical|center_horizontal"
android:layout_marginBottom="18px"
android:layout_marginTop="60px"
android:background="@drawable/button_background_selector"
android:textColor="@drawable/button_text_selector"
/>
<Button
android:id="@+id/bn2"
android:layout_width="520px"
android:layout_height="72px"
android:text="取消"
android:textSize="28px"
android:gravity="center_vertical|center_horizontal"
android:background="@drawable/button_background_selector"
android:textColor="@drawable/button_text_selector"
/>
</LinearLayout>
</LinearLayout>
其中的Button,以第一个为例:
<Button
android:id="@+id/bn1"
android:layout_width="520px"
android:layout_height="72px"
android:text="保存"
android:textSize="28px"
android:gravity="center_vertical|center_horizontal"
android:layout_marginBottom="18px"
android:layout_marginTop="60px"
android:background="@drawable/button_background_selector"
android:textColor="@drawable/button_text_selector"
/>
其中button_background_selector为xml文件,可在res中新建drawable文件夹并将其放置到其中,具体为
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@drawable/button_focus" > </item>
<item android:drawable="@drawable/button_default" > </item>
</selector>
其中button_focus以及button_default也分别为xml文件,放在drawalbe文件夹中
button_focus.xml的xml文件具体为:
<?xml version="1.0" encoding="utf-8"?>
<color xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/button_focus_color">
</color>
button_default.xml的xml文件具体为:
<?xml version="1.0" encoding="utf-8"?>
<color xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/button_default_color">
</color>
其中的button_focus_color与button_default_color为values文件夹中新建的color.xml文件中定义的,具体代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="button_focus_color">#004B64</color>
<color name="button_default_color">#3B3B3B</color>
<color name="text_focus_color">#ffffff</color>
<color name="text_default_color">#e6e6e6</color>
</resources>
方法二:采用9patch图片做button背景图片的方法
在factory_reset这个xml文件中,其具体xml文件为:(跟方法一中的代码是一样的,方法二只是改变了button_background_selector这个xml文件里的东西)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="560px"
android:layout_height="348px"
android:background="#212121"
android:orientation="vertical"
android:layout_gravity="center_vertical|center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <!-- 怎样设置 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|bottom"
android:text="确定要恢复出厂设置吗?"
android:textColor="#e6e6e6"
android:textSize="34px"
android:paddingTop="68px"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal" >
<Button
android:id="@+id/bn1"
android:layout_width="520px"
android:layout_height="72px"
android:text="保存"
android:textSize="28px"
android:gravity="center_vertical|center_horizontal"
android:layout_marginBottom="18px"
android:layout_marginTop="60px"
android:background="@drawable/button_background_selector"
android:textColor="@drawable/button_text_selector"
/>
<Button
android:id="@+id/bn2"
android:layout_width="520px"
android:layout_height="72px"
android:text="取消"
android:textSize="28px"
android:gravity="center_vertical|center_horizontal"
android:background="@drawable/button_background_selector"
android:textColor="@drawable/button_text_selector"
/>
</LinearLayout>
</LinearLayout>
其中的Button,以第一个为例:
<Button
android:id="@+id/bn1"
android:layout_width="520px"
android:layout_height="72px"
android:text="保存"
android:textSize="28px"
android:gravity="center_vertical|center_horizontal"
android:layout_marginBottom="18px"
android:layout_marginTop="60px"
android:background="@drawable/button_background_selector"
android:textColor="@drawable/button_text_selector"
/>
其中button_background_selector为xml文件,可在res中新建drawable文件夹并将其放置到其中,具体为:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@drawable/button_pressed" > </item>
<item android:drawable="@drawable/button_normal" > </item>
</selector>
由于这里给出了button_pressed跟button_normal这两个9patch背景图片,所以可以直接用android:drawable=“两张9patch图片的位置”来改变button的背景。
其中在res中新建了drawable文件夹,并在里边放了button_pressed跟button_normal这两个9patch图片,如下图所示:
button_normal.9.png button_pressed.9.png
xml中,button改变背景颜色方法的更多相关文章
- iOS 创建多个button实现点击改变背景颜色
工程中需要实现与UISegmentedControl效果相似的一排一共十个button,如下图.但是SegmentedControl修改不太方便,就用button替代, 循环创建十个button,点击 ...
- Idea中更改主题后xml配置文件局部黄色背景颜色去除
相信很多小伙伴和我一样一样的,喜欢更换Idea的主题,但是细心的小伙伴就发现了,每次更改主题后xml配置文件就会局部产生黄色背景颜色,对于强迫症患者真的是够了,网上也有部分文章,但是不够详细,也跟Id ...
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
在Qt中设置widget背景颜色或者图片方法很多种:重写paintEvent() , 调色板QPalette , 样式表setStyleSheet等等. 但是各种方法都有其注意事项,如果不注意则很容易 ...
- QT中设置窗口背景颜色
QWidget是所有用户界面对象的基类,这意味着可以用同样的方法为其它子类控件改变背景颜色. Qt中窗口背景的设置,下面介绍三种方法. 1.使用QPalette 2.使用Style Sheet 3.绘 ...
- [JS9] document's bgColor改变背景颜色
<HTML> <HEAD> <TITLE>设置背景颜色</TITLE> </HEAD> <BODY> <CENTER> ...
- 怎么给button设置背景颜色?【Android】
怎么给button设置背景颜色?[Android] 怎么给button设置背景颜色?[Android] 现在我想给按钮添加背景颜色,怎么做 1.android:background="@an ...
- OpenGL的glClearColor和glClear改变背景颜色
OpenGL的glClearColor和glClear改变背景颜色 结合以下两个函数void glClearColor(GLclampf red, GLclampf green, GLclamp ...
- button改变背景与文字颜色
1.定义/zhsh/res/color/txt_guide_selector.xml <?xml version="1.0" encoding="utf-8&quo ...
- jquery动态改变背景颜色插件
GETHUB下载地址 背景颜色用animate方法时时无法改变颜色的 所以要使用插件进行补充. 用法: <!DOCTYPE html> <html> <head> ...
随机推荐
- LRU Cache 解答
Question Design and implement a data structure for Least Recently Used (LRU) cache. It should suppor ...
- Windows多线程同步系列之三-----事件对象
事件是一个内核事件,内核事件是什么呢,我理解也不深入也不好说,暂且理解为一个内核维护的数据类型吧通过内核事件同步主要 的方法是对事件的信号有和无来进行同步. 比如当我们一个线程进入一段临界代码(独占代 ...
- OpenWrt for vmware 从openwrt.org下载10.03.1 或是自己下载最新的源码进行编译生成x86 vmdk格式
1,直接从OpenWrt.org官网下载 http://downloads.openwrt.org/backfire/10.03.1/x86_generic/ 更新OpenWrt在线软件源 opkg ...
- HTML5 Canvas动画效果演示 - 流浪的鱼 - 博客频道 - CSDN.NET
HTML5 Canvas动画效果演示 - 流浪的鱼 - 博客频道 - CSDN.NET HTML5 Canvas动画效果演示
- [转]使用Navicat for Oracle工具连接oracle的
使用Navicat for Oracle工具连接oracle的 这是一款oracle的客户端的图形化管理和开发工具,对于许多的数据库都有支持.之前用过 Navicat for sqlserver,感觉 ...
- 网站SEO优化中内部链接的优化
重要性:内链有效的优化能够间接的提高某页面的权重达到搜索排名靠前的效果.同时有效的带领搜索引擎蜘蛛对整站进行抓取. 网站头部导航: 这个导航称为'网站主导航',当用户来到网站需要给他们看到的内容.也就 ...
- 编程获取linux的CPU使用的内存使用情况
Linux可用下top.ps命令检查当前的cpu.mem用法.下面简单的例子: 一.采用ps查看资源消耗的过程 ps -aux 当您查看进程信息,第三列是CPU入住. [root@localhost ...
- C#的简单的Windows Service 创建与安装
注意事项: 1. 添加调试代码 入口: 服务: 2. 再服务界面右键添加安装程序 3. 修改安装程序属性(Account) 4. 修改服务安装属性(DelayedAutoStart,ServiceNa ...
- AppSettings
1.winform中读写配置文件appSettings 一节中的配置. #region 读写配置文件 /// <summary> /// 修改配置文件中某项的值 /// </summ ...
- Graham算法—二维点集VC++实现
一.凸包定义 通俗的说就是:一组平面上的点,求一个包含所有点的最小凸多边形,这个最小凸多边形就是凸包. 二.Graham算法思想 概要:Graham算法的主要思想就是,最终形成的凸包,即包围所有点的凸 ...