Understanding of declare-styleable, style, Theme,?attr/ in Android

Keywords: Mobile Android Attribute

1. declare-style leable defines some attributes, writes some identical attributes together, defines a name, or can be directly defined. In the following two ways, just use declare-style leable to organize similar attributes together. When customizing View, there is also a slight difference in the way attribute values are obtained.

    <attr format="reference" name="drawerArrowStyle"/>
    <attr format="dimension" name="height"/>
    <attr format="boolean" name="isLightTheme"/>
    <attr format="string" name="title"/>
<declare-styleable name="ActionBar">
    <attr name="navigationMode">
            <enum name="normal" value="0"/>
            <enum name="listMode" value="1"/>
            <enum name="tabMode" value="2"/>
     </attr>
    <attr name="title"/>
    <attr format="string" name="subtitle"/>
</declare-styleable>

(1) The acquisition methods are as follows:

TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.yourAttr, typedValue, true);

(2) The acquisition methods are as follows:

//Get custom properties.
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView);
//Get font size, default size is 16dp
fontSize = (int) ta.getDimension(R.styleable.CustomView_font, 16);

2. Theme theme, which is also the meaning of style, belongs to style. The system theme only defines a general style. In different Theme, the attribute value of style is different, which becomes a different theme.

<style name="Theme.Black">
        <item name="windowBackground">@color/black</item>
        <item name="colorBackground">@color/black</item>
</style>
 <style name="Theme.Light">
        <item name="windowBackground">@drawable/screen_background_selector_light</item>
        <item name="colorBackground">@color/background_light</item>
</style>

3.? attr / refers to the attributes of the system. Generally, the attributes of the system are defined in the theme of APP. Find the default actionBarSize value of the Android system.

    <!-- Base application theme. -->
    <style name="AppThemeBase" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:divider">@drawable/ic_list_divider</item>
        <item name="android:dividerHeight">@dimen/division_line</item>
        <item name="android:listDivider">@drawable/ic_list_divider</item>
        <item name="android:homeAsUpIndicator">@drawable/ic_arrow_back_white_24dp</item>
        <item name="styleLoadingLayout">@style/LoadingLayoutStyle</item>
    </style>

For example, to find app:navigationIcon="?attr/homeAsUpIndicator", you can finally find it in the system theme.

<style name="Base.V7.Theme.AppCompat" parent="Platform.AppCompat">
        <item name="windowNoTitle">false</item>
        <item name="windowActionBar">true</item>
        <item name="windowActionBarOverlay">false</item>
        <item name="windowActionModeOverlay">false</item>
        <item name="actionBarPopupTheme">@null</item>
        <item name="homeAsUpIndicator">@drawable/abc_ic_ab_back_material</item>
        <item name="actionBarSize">@dimen/abc_action_bar_default_height_material</item>
</style>	

<dimen name="abc_action_bar_default_height_material">56dp</dimen>

Above is the understanding of the four, here is a record, in order to avoid forgetting.

Posted by unrelenting on Wed, 23 Jan 2019 01:12:15 -0800