Ali*_*zar 6 android colors button
我的应用程序的布局文件中的按钮有以下代码
<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)
默认情况下,按下按钮时,其高亮颜色为蓝色.如何将其更改为我想要的颜色?(我希望它是深红色的)
Tia*_*ida 18
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/your_color"
android:state_pressed="true" />
<item android:drawable="@color/your_color"
android:state_focused="true" />
</selector>
Run Code Online (Sandbox Code Playgroud)
您可以将其创建为res/drawables文件夹中的文件,然后在按钮中将其用作背景.假设您调用了该文件"my_button.xml",然后可以像这样使用它:
<Button
android:background="@drawable/my_button"
Run Code Online (Sandbox Code Playgroud)
或者像这样:
my_button.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_button));
Run Code Online (Sandbox Code Playgroud)
您的颜色可以在res/values文件夹中的colors.xml中定义.如果您没有此文件,则可以创建它(android会识别它).这是一个很好的做法,但您也可以在上面的代码中用#DC143C替换your_color.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="your_color">#DC143C</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
请注意,此颜色已设置为深红色.
您还可以为背景添加图像,将"@ color/your_color"替换为"@ drawable/your_image".
有关更多信息,您可以在stackoverflow中关注此链接.
小智 6
在drawable文件夹中创建以下xml文件.并将您的按钮背景设置为此drawable.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Image display in background in select state -->
<item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
<!-- Image display in background in select state -->
<item android:drawable="@drawable/button_pressed" android:state_focused="true"/>
<!-- Default state -->
<item android:drawable="@drawable/button"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
24679 次 |
最近记录: |