xml中设置button的背景颜色

摘要:
在画几个设置界面,用到了button控件,对于button空间的背景色在不同状态下的颜色改变方法,做了一下尝试,发现了两种背景颜色改变的方法,就总结了下。--怎样设置--˃其中的Button,以第一个为例:其中button_background_selector为xml文件,可在res中新建drawable文件夹并将其放置到其中,具体为button_default.xml的xml文件具体为:˂?

在画几个设置界面,用到了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_selectorxml文件,可在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.xmlxml文件具体为:

<?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.xmlxml文件具体为:

<?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_colorbutton_default_colorvalues文件夹中新建的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_selectorxml文件,可在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_pressedbutton_normal这两个9patch背景图片,所以可以直接用android:drawable=“两张9patch图片的位置”来改变button的背景。

其中在res中新建了drawable文件夹,并在里边放了button_pressedbutton_normal这两个9patch图片,如下图所示:

免责声明:文章转载自《xml中设置button的背景颜色》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Delphi:RzPageControl(pagecontrol)实现多标签的动态添加,切换,关闭windows 服务实现定时任务调度(Quartz.Net)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android2.3.7源码结构分析

对Andorid系统进行分析或者系统功能定制的时候,我们经常需要在众多文件中花费大量时间定位所需关注的部分。为了减轻这部分枯燥而不可避免的工作,本文对2.3.7版本的源码结构进行了简单分析。希望对刚加入安卓大军的朋友们有所帮助。下图为2.3.7_r1版本的根目录结构:Makefile (makefile文件) bionic (bionic C库,...

关于XML的技术详情----XML定义 用途 工作原理及未来

  一、XML的定义和用途       可扩展标记语言XML(eXtensible Markup Language)是一种简单灵活的文本格式的可扩展标记语言,起源于SGML(Standard Generalized Markup Language),是SGML的一个子集合,也就是SGML的一个简化版本,非常适合于在Web上或者其它多种数据源间进行数据的交换...

AirtestIDE高级功能

本篇文章基于AirtestIDE1.2.9 前期回顾: AirtestIDE基本功能(一) AirtestIDE基本功能(二) 一、图片编辑器 双击代码编辑区里的图片,就可以打开该图片的编辑界面 Snapshot+Recognition 点击左上按钮,对当前手机屏幕截图,并识别图片是否在截图中,识别到后会红圈标出,并在左下显示识别方法和可信度 filen...

分析各种Android设备屏幕分辨率与适配

一. 数据采集    源码GitHub地址 :  -- SSH : git@github.com:han1202012/DisplayTest.git; -- HTTP : https://github.com/han1202012/DisplayTest;   . 使用下面的程序运行在不同设备上 :   package shuliang.han.dis...

博客园样式美化(背景图片、公告栏头像、看板娘、鼠标特效、网易云音乐)

一、博客园样式美化 1、功能: 背景图片 公告栏头像 看板娘 鼠标特效 音乐插件 二、博客园设置步骤 1、准备工作 (1)、进博客园管理-设置页面使用如下皮肤。 (2)、申请公告栏JS权限,博客侧边栏公告(支持HTML代码)处点击申请, 理由可以写,制作个人风格博客等。审批很快的。 (3)、打开公告栏,进博客园管理-选项页面勾选公告。本人就放入了一个头像...

使用C#读写结构化的二进制文件

最近工作上遇到一个问题,就是有将近200万个CSV文件,每个CSV文件包含了成千上万条实验数据,CSV以一个不连续的整数值作为文件名,比如:1.CSV、2.CSV、3.CSV、5.CSV等等。另外又有200万个XML文件,每个XML文件的文件名与CSV的文件名一一对应,在这些XML文件中,定义了所对应的CSV实验数据文件的实验描述信息(比如实验名称、实验类...