我正在尝试使用选择器创建一个按钮,我的按钮可以具有以下状态:
根据上述状态。我需要操纵按钮:
该按钮从我被禁用开始,因此它应该具有禁用的textColor和禁用的按钮背景。但我可以看到默认的textColor(在样式中指定)和没有背景图像!
这是我的选择器button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:state_enabled="false"
android:textColor="#9D9FA2"
android:drawable="@drawable/button" />
<item android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/button_pressed"/>
<item android:state_pressed="true"
android:state_enabled="false"
android:textColor="#9D9FA2"
android:drawable="@drawable/button"/>
<item android:state_pressed="false"
android:state_enabled="true"
android:drawable="@drawable/button"/>
</selector>
这是我的layout.xml中的按钮声明
<Button android:id="@+id/reserve_button"
android:text="@string/reserve_button"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:paddingRight="15dp"
android:layout_gravity="left"
style="@style/buttonStyle"
android:background="@drawable/button_selector" />
最后这是我的风格(设置我的默认textColor)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="buttonStyle">
<item name="android:textStyle">bold</item>
<item name="android:textColor">#282780</item>
<item name="android:textSize">18sp</item>
</style>
</resources>
请帮忙!
答案 0 :(得分:210)
您还需要为识别不同状态的文字颜色创建ColorStateList
。
执行以下操作:
在res\color
中创建另一个名为text_color.xml
的XML文件。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- disabled state -->
<item android:state_enabled="false" android:color="#9D9FA2" />
<item android:color="#000"/>
</selector>
在style.xml
中,按如下方式对该text_color.xml
文件进行引用:
<style name="buttonStyle" parent="@android:style/Widget.Button">
<item name="android:textStyle">bold</item>
<item name="android:textColor">@color/text_color</item>
<item name="android:textSize">18sp</item>
</style>
这可以解决您的问题。
答案 1 :(得分:5)
1.在/ res /文件夹中创建一个颜色文件夹,在xml中创建颜色文件夹:
text_color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- disabled state -->
<item android:state_enabled="false" android:color="#776678" />
<item android:color="#ffffff"/>
</selector>
2.现在创建一个xml布局: -
<Button
android:id="@+id/button_search"
android:layout_width="652dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:background="@android:color/transparent"
android:text="Hello Bhaskar"
android:textColor="@color/text_color_selector"/>
答案 2 :(得分:3)
最简单的解决方案是在我看到here
时将滤色器设置为和按钮的背景图像您可以执行以下操作:
if ('need to set button disable')
button.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
else
button.getBackground().setColorFilter(null);
希望我能帮助别人......
答案 3 :(得分:-1)
<Button android:id="@+id/reserve_button"
android:text="@string/reserve_button"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:paddingRight="15dp"
android:layout_gravity="left"
style="@style/buttonStyle"
android:background="@drawable/button_selector" />
我在布局xml中看不到你的按钮。将其添加到按钮布局中。
android:enabled="false"
所以你的按钮布局将是,
<Button android:id="@+id/reserve_button"
android:text="@string/reserve_button"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:enabled="false"
android:paddingRight="15dp"
android:layout_gravity="left"
style="@style/buttonStyle"
android:background="@drawable/button_selector" />
答案 4 :(得分:-1)
您可以创建颜色列表
文件位置:
res/color/filename.xml
文件名将用作资源ID。
资源参考:
在Java中:R.color.filename
在XML中:@[package:]color/filename
<强>语法:强>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="hex_color"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</selector>
示例:强>
保存在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>
此布局XML将颜色列表应用于视图:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textColor="@color/button_text" />