2012-04-04 71 views
1

我有一些可以绘制按钮,复选框,进度条等项目的绘图,我想定义这些可绘制的对象,用来显示我应用程序使用的所有按钮的默认样式。在应用程序中的自定义视图(安卓)

这可能没有在每个视图中单独声明样式?

如果答案是使用应用程序的主题,我会欣赏一个例子(例如改变所有按钮的样式),因为我不明白如何定义它。

在此先感谢

回答

2

的答案是使用一个主题,您所期望。良好的文档可以在Android开发者网站上找到:http://developer.android.com/guide/topics/ui/themes.html

对于按钮,你可以这样做: 定义一个新的主题:

<style name="MyTheme" parent="@android:style/Theme.Holo"> 
    <item name="android:buttonStyle">@style/button</item> 
</style> 

凡按钮样式定义为:

<style name="button" parent="@android:style/Widget.Button"> 
    <item name="android:background">@drawable/button_background_selector</item> 
    <item name="android:textColor">@drawable/button_text_selector</item> 
</style> 

button_background_selector的定义如下:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/button_pressed" android:state_pressed="true"></item> 
    <item android:drawable="@drawable/button_disabled" android:state_enabled="false"></item> 
    <item android:drawable="@drawable/button_default"></item> 

</selector> 

所有这些drawable可以是你的图像 - button_pressed可以是可绘制文件夹中的png。 您也可以将主题应用到您的应用程序清单如下:

<application 
    android:theme="@style/MyTheme" > 
</application> 

希望这有助于:)

相关问题