Android - default button style

前端 未结 2 1749
说谎
说谎 2020-12-12 13:05

Question: Where can I find default styles xml with hexadecimal codes of colors?

I\'m looking for Style \'buttonStyle\' and other default styles witc

相关标签:
2条回答
  • 2020-12-12 13:46

    Understanding how Android styles do work can be a little bit messy.

    I will try to explain how the basic work flow would be, based on an example.

    Let's assume you want to know what the default background for buttons is. This can be either a simple color (unlikely) or a drawable (there are many different types of drawables).

    Android has Themes. A theme basically defines which style is applied to which widget. Therefore, our first step is to find the default android theme.

    You find it under android-sdk\platforms\android-15\data\res\values\themes.xml

    In this theme file, search for button.

    You will find something like this:

    <!-- Button styles -->
    
    <item name="buttonStyle">@android:style/Widget.Button</item>
    

    This means that the theme applies the style Widget.Button to buttons.

    Ok, now let's find the style Widget.Button.

    All default Android style are defined in the file android-sdk\platforms\android-15\data\res\values\styles.xml

    Now search for Widget.Button

    You will find something like this:

    <style name="Widget.Button">
        <item name="android:background">@android:drawable/btn_default</item>
        <item name="android:focusable">true</item>
        <item name="android:clickable">true</item>
        <item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
        <item name="android:textColor">@android:color/primary_text_light</item>
        <item name="android:gravity">center_vertical|center_horizontal</item>
    </style>
    

    The interesting line is:

    <item name="android:background">@android:drawable/btn_default</item>

    This means that there is a drawable called btn_default set as button background.

    Now we need to find a file named btn_default.* in one of the drawable folders under android-sdk\platforms\android-15\data\res.

    This can be either an image (very unlikely) or a xml file like btn_default.xml.

    After a little bit searching you will find the file android-sdk\platforms\android-15\data\res\drawable\btn_default.xml

    It contains something like this:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/btn_default_normal" />
        <item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/btn_default_normal_disable" />
        <item android:state_pressed="true" android:drawable="@drawable/btn_default_pressed" />
        <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/btn_default_selected" />
        <item android:state_enabled="true" android:drawable="@drawable/btn_default_normal" />
        <item android:state_focused="true" android:drawable="@drawable/btn_default_normal_disable_focused" />
        <item android:drawable="@drawable/btn_default_normal_disable" />
    </selector>
    

    Now you have to understand that this is a selector drawable (one of the many drawable types). This selector chooses different backgrounds, based on the buttons state. For example, if the buttons is pressed, it has a different background.

    No let's look at the default state.

    <item android:state_enabled="true" android:drawable="@drawable/btn_default_normal" />

    It applies a drawable called btn_default_normal.

    Now we need to find this drawable.

    Again, we need to find a file named btn_default_normal.* in one of the drawable folders under android-sdk\platforms\android-15\data\res.

    This can be again either an image or a xml file like btn_default_normal.xml.

    You will find multiple files called 'btn_default_normal.9.png' in different drawable folders for different resolutions.

    :) Now you know that btn_default_normal.9.png is set as button background.

    0 讨论(0)
  • 2020-12-12 13:52

    You can find default styles of android widgets when you declare in XML:

    style="@android:style/Widget.Button" - standard Button
    style="@android:style/Widget.TextView" - standard TextView
    

    For example this is style of default button:

        <style name="Widget.Button">
            <item name="android:background">@android:drawable/btn_default</item>
            <item name="android:focusable">true</item>
            <item name="android:clickable">true</item>
            <item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
            <item name="android:textColor">@android:color/primary_text_light</item>
            <item name="android:gravity">center_vertical|center_horizontal</item>
        </style>
    

    background of default button:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/btn_default_normal" android:state_enabled="true" android:state_window_focused="false"/>
        <item android:drawable="@drawable/btn_default_normal_disable" android:state_enabled="false" android:state_window_focused="false"/>
        <item android:drawable="@drawable/btn_default_pressed" android:state_pressed="true"/>
        <item android:drawable="@drawable/btn_default_selected" android:state_enabled="true" android:state_focused="true"/>
        <item android:drawable="@drawable/btn_default_normal" android:state_enabled="true"/>
        <item android:drawable="@drawable/btn_default_normal_disable_focused" android:state_focused="true"/>
        <item android:drawable="@drawable/btn_default_normal_disable"/>
    </selector> 
    

    You can get the default color with the help of any graphics editor: path to default button NinePatch ..\android-sdk\platforms\android-13\data\res\drawable-hdpi

    0 讨论(0)
提交回复
热议问题