set button background to color
来源:互联网 发布:纪念日软件iphone 编辑:程序博客网 时间:2024/05/21 03:20
Android button with different background colors
i want to change the background-color of a button using a selector-xml-file. My approach is basically the one from the example at the bottom this page:http://developer.android.com/guide/topics/resources/color-list-resource.html
i have a res/color/button_text.xml which looks like this:
<?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>
and my layout contains the following code:
<Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button_text" **android:background="@color/button_text"** />
(** is only there to show you that i use android:background instead of android:textcolor)
this code crashes. it says "Binary XML file line #4 tag requires 'drawable' attribute or child tag defining drawable. But if I try it with android:textColor as described in the above link it works fine. So it has to be the background issue. I don't want to create a 9patch-png if it's not necessary (basically i just need a "clickable" rectangle so i use a button with a colored background)
4 Answers
As your error states, you have to define drawable attibute for the items (for some reason it is required when it comes to background definitions), so:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@color/red"/> <!-- pressed --> <item android:state_focused="true" android:drawable="@color/blue"/> <!-- focused --> <item android:drawable="@color/black"/> <!-- default --></selector>
Also note that drawable attribute doesn't accept raw color values, so you have to define the colors as resources. Createcolors.xml file at res/values folder:
<?xml version="1.0" encoding="utf-8"?><resources> <color name="black">#000</color> <color name="blue">#00f</color> <color name="red">#f00</color></resources>
In the URL you pointed to, the button_text.xml is being used to set the textColor attribute.That it is reason they had the button_text.xml in res/color folder and therefore they used @color/button_text.xml
But you are trying to use it for background attribute. The background attribute looks for something in res/drawable folder.
check this i got this selector custom button from the internet.I dont have the link.but i thank the poster for this.It helped me.have this in the drawable folder
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape> <gradient android:startColor="@color/yellow1" android:endColor="@color/yellow2" android:angle="270" /> <stroke android:width="3dp" android:color="@color/grey05" /> <corners android:radius="3dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item android:state_focused="true" > <shape> <gradient android:endColor="@color/orange4" android:startColor="@color/orange5" android:angle="270" /> <stroke android:width="3dp" android:color="@color/grey05" /> <corners android:radius="3dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item> <shape> <gradient android:endColor="@color/white1" android:startColor="@color/white2" android:angle="270" /> <stroke android:width="3dp" android:color="@color/grey05" /> <corners android:radius="3dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item></selector>
And i used in my main.xml layout like this
<Button android:id="@+id/button1" android:layout_alignParentLeft="true" android:layout_marginTop="150dip" android:layout_marginLeft="45dip" android:textSize="7pt" android:layout_height="wrap_content" android:layout_width="230dip" android:text="@string/welcomebtntitle1" android:background="@drawable/custombutton"/>
Hope this helps. Vik is correct.
EDIT : Here is the colors.xml
<?xml version="1.0" encoding="utf-8"?><resources> <color name="yellow1">#F9E60E</color> <color name="yellow2">#F9F89D</color> <color name="orange4">#F7BE45</color> <color name="orange5">#F7D896</color> <color name="blue2">#19FCDA</color> <color name="blue25">#D9F7F2</color> <color name="grey05">#ACA899</color> <color name="white1">#FFFFFF</color> <color name="white2">#DDDDDD</color></resources>
- set button background to color
- set transparent button image and background color to button
- Set the background color
- ios set UINavigationBar background color
- gedit set the color of background
- Set terminal text color and transparent background
- angular ngDialog background color to be transplant
- 一种设置Button background Color 背景颜色的方法
- background-color
- how to set UITableViewCell background image
- UILabel How to set background image
- How To Change the Background Color of a Tab Control
- How to change tooltip background color in Unity?
- how to change the background color of actionbar
- How to customize the background color of a UITableViewCell?
- qt 之 How to Change the Background Color of QWidget
- 优化WebView nativeOnDraw failed; clearing to background color及其根本原因
- Using Profile:Java Color Scheme to set Forms Color
- mysql-5.6 linux安装方法详细记录
- 用CSerialPort类收不到0x11, 0x13问题及解决办法
- 室内地图:Indoor map for iOS & Android without GPS and Internet
- Mina框架学习笔记(三)
- C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast
- set button background to color
- 软件概要设计
- 暑假编程训练---D:打印金字塔
- URL特殊字符转义
- LINUX0.11内核阅读笔记
- 有娃的学,無娃的存,娃大的轉!
- 将主机IDS OSSEC日志文件存入MYSQL的方法
- URL中,哪些字符需要编码?
- linux C/C++ 连接oracle
item android:color="@color/black"
.......... I propose it should read item android:drawable="@color/black" ........... – user609727Feb 9 '11 at 12:22