Custom Views in the application (Android)

微笑、不失礼 提交于 2019-12-08 06:32:43

问题


I've a drawable for some items like buttons, checkboxes, progress bars and I'd like to define those drawables for beeing the default style of all the buttons my app uses.

Is that possible without declaring the style in each View individually?

If the answer is to use a Theme for the app I'd appreciate an example (changing the style of all the buttons for example) because I don't understand how to define it.

Thanks in advance


回答1:


The answer is to use a theme as you were expecting. Good documentation can be found on android developer site: http://developer.android.com/guide/topics/ui/themes.html

For the button you can do like this: Define a new theme:

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

Where the button style is defined as:

<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>

The button_background_selector is defined like this:

<?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>

All those drawables can be your images - button_pressed can be a png in the drawable folders. You also have to apply the theme to your application in the manifest like this:

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

Hope this helps :)



来源:https://stackoverflow.com/questions/10017765/custom-views-in-the-application-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!