use*_*308 9 android coding-style pressed button
有没有办法在按下按钮时将样式应用于按钮?
如果我在style.xml中有样式:
<resources>
<style name="test">
<item name="android:textStyle">bold</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
button.xml中的选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/test_pressed"
style="@style/test"
android:state_pressed="true"/>
<item android:drawable="@drawable/test_focused"
android:state_focused="true"/>
<item android:drawable="@drawable/test_normal"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
那么我如何在布局中引用button.xml?
<Button
...
android:???="button"/>
Run Code Online (Sandbox Code Playgroud)
谢谢!
Romain Guy建议不可能:
http://code.google.com/p/android/issues/detail?id=8941
"选择器仅适用于绘图,而不适用于文本外观.目前还没有计划实现这一目标."
您可以使用XML按钮定义来实现此目的.
在drawable文件夹中创建XML文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/green_button" />
<!-- <item android:state_focused="true"
android:drawable="@drawable/button_focused" /> -->
<item android:drawable="@drawable/black_button" />
</selector>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这允许您定义不同的按钮图像以用于不同的状态(black_button,green_button等也应该是您的drawable文件夹中的.PNG文件)
现在,您可以从layout.xml文件中将按钮背景设置为指向按钮选择器:
<Button android:text="Play" android:id="@+id/playBtn"
android:background="@drawable/button_selector"
android:textColor="#ffffff" />
Run Code Online (Sandbox Code Playgroud)
然后可以像任何图像文件一样从可绘制文件夹中引用选择器XML.
归档时间: |
|
查看次数: |
24102 次 |
最近记录: |