如何设置Button的颜色

如何设置Button的颜色,第1张

css设置就行了,背景色 *** 作

button{

background-color:red

}

button就是对按钮元件定义。

background-color背景颜色定义。

貌似你写的有问题吧,不用写type="button"这句,因为本来就是button标签。

<button value="ch1" >选项一</button>

如果不加class或者id,可以这样写:

button{ background-color:#ff0000}

<button class="btn1" value="ch1" >选项一</button>

如果加个class,可以这样写:

.btn1{ background-color:#ffff00}

可以使用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"


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11003641.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-12
下一篇 2023-05-12

发表评论

登录后才能评论

评论列表(0条)

保存