我想使用selector-xml文件更改按钮的背景颜色.我的方法基本上是本页底部示例中的方法:http://developer.android.com/guide/topics/resources/color-list-resource.html
我有一个res/color/button_text.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>
Run Code Online (Sandbox Code Playgroud)
我的布局包含以下代码:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
**android:background="@color/button_text"** />
Run Code Online (Sandbox Code Playgroud)
(**只是告诉你我使用android:background而不是android:textcolor)
这段代码崩溃了.它说"二进制XML文件行#4标签需要'drawable'属性或子标签定义drawable.但如果我尝试使用android:textColor,如上面的链接所述,它工作正常.所以它必须是背景问题.我如果没有必要,我不想创建一个9patch-png(基本上我只需要一个"可点击"的矩形,所以我使用带有彩色背景的按钮)
随着新的Android支持更新,矢量drawables获得向后兼容性.我有一个带有各种路径的矢量图像.我希望通过单击按钮或基于输入值以编程方式更改路径的颜色.是否可以访问矢量路径的name参数?然后改变颜色.
我的应用程序的布局文件中的按钮有以下代码
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Start updating" />
Run Code Online (Sandbox Code Playgroud)
默认情况下,按下按钮时,其高亮颜色为蓝色.如何将其更改为我想要的颜色?(我希望它是深红色的)
我创建了一个Button b
背景颜色为黑色.当我点击它时,我希望它的颜色只有在我手指上的时候变为绿色,即我一直关注它.