zoukankan      html  css  js  c++  java
  • theme- 工作原理

    首先看一下theme中的设置,代码如下

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <style name="Theme.Funui" parent="Theme.Holo.Light">
            <item name="isFunui">true</item>
            <item name="colorForeground">@android:color/bright_foreground_funui</item>
            <item name="colorForegroundInverse">@android:color/bright_foreground_funui_inverse</item>
            <item name="colorBackground">@android:color/background_funui</item>
            <item name="colorBackgroundCacheHint">@android:drawable/background_cache_hint_selector_funui</item>
            <item name="disabledAlpha">0.5</item>
            <item name="backgroundDimAmount">0.6</item>
    
            <item name="colorPressedHighlight">@color/funui_gray_light</item>
            <item name="colorLongPressedHighlight">@color/funui_gray_bright</item>
            <item name="colorFocusedHighlight">@color/funui_blue_dark</item>
            <item name="colorMultiSelectHighlight">@color/funui_green_light</item>
            <item name="colorActivatedHighlight">@color/funui_blue_dark</item>
    
            <!-- Text styles -->
            <item name="textAppearance">@android:style/TextAppearance.Funui</item>
            <item name="textAppearanceInverse">@android:style/TextAppearance.Funui.Inverse</item>
    
            <item name="textColorPrimary">@android:color/primary_text_funui</item>
            <item name="textColorSecondary">@android:color/secondary_text_funui</item>
            <item name="textColorTertiary">@android:color/tertiary_text_funui</item>
            <item name="textColorPrimaryInverse">@android:color/primary_text_funui_inverse</item>
            <item name="textColorSecondaryInverse">@android:color/secondary_text_funui_inverse</item>
            <item name="textColorTertiaryInverse">@android:color/tertiary_text_funui_inverse</item>
            <item name="textColorPrimaryDisableOnly">@android:color/primary_text_disable_only_funui</item>
            <item name="textColorPrimaryInverseDisableOnly">@android:color/primary_text_disable_only_funui_inverse</item>
            <item name="textColorPrimaryNoDisable">@android:color/primary_text_nodisable_funui</item>
            <item name="textColorSecondaryNoDisable">@android:color/secondary_text_nodisable_funui</item>
            <item name="textColorPrimaryInverseNoDisable">@android:color/primary_text_nodisable_funui_inverse</item>
            <item name="textColorSecondaryInverseNoDisable">@android:color/secondary_text_nodisable_funui_inverse</item>
            <item name="textColorHint">@android:color/hint_foreground_funui</item>
            <item name="textColorHintInverse">@android:color/hint_foreground_funui_inverse</item>
            <item name="textColorSearchUrl">@android:color/search_url_text_funui</item>
            <item name="textColorHighlight">@android:color/highlighted_text_funui</item>
            <item name="textColorHighlightInverse">@android:color/highlighted_text_funui_inverse</item>
            <item name="textColorLink">@android:color/funui_blue_light</item>
            <item name="textColorLinkInverse">@android:color/funui_blue_light</item>
            <item name="textColorAlertDialogListItem">@android:color/primary_text_funui</item>
         <item name="checkboxStyle">@android:style/Widget.Funui.CompoundButton.CheckBox</item>
             <item name="searchViewTextColor">@color/searchview_text_color</item>
    <item name="textAppearanceLarge">@android:style/TextAppearance.Funui.Large</item> <item name="textAppearanceMedium">@android:style/TextAppearance.Funui.Medium</item> <item name="textAppearanceSmall">@android:style/TextAppearance.Funui.Small</item> <item name="textAppearanceLargeInverse">@android:style/TextAppearance.Funui.Large.Inverse</item> <item name="textAppearanceMediumInverse">@android:style/TextAppearance.Funui.Medium.Inverse</item>
    </Resources>

    然后我们查找一下其中的一个属性<item name="checkboxStyle">@android:style/Widget.Funui.CompoundButton.CheckBox</item>

    我们先不说后面的@android:style/Widget.Funui.CompoundButton.CheckBox 就先看一下这个checkboxStyle ,搜索以后,发现如下代码

    public class Button extends TextView {
        public Button(Context context) {
            this(context, null);
        }   
    
        public Button(Context context, AttributeSet attrs) {
    

    //引用的地方
            this(context, attrs, com.android.internal.R.attr.buttonStyle); } public Button(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onInitializeAccessibilityEvent(AccessibilityEvent event) { super.onInitializeAccessibilityEvent(event); event.setClassName(Button.class.getName()); } @Override public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { super.onInitializeAccessibilityNodeInfo(info); info.setClassName(Button.class.getName()); } }

    这就是第一种情况,也就是,属性是直接在代码中添加的,有点类似于我们的自定义属性。不过呢,这个明显的是,直接添加的一个theme,他的作用就跟我们直接创建一个aaa的style,然后new (.....,R.style.aaa)一样的,

    只不过它是应用整个activity的

    另外我们还发现了它出现在另外一个地方res/values/attr.xml中,如下

    <!--类似于一个声明-->
    <attr name="checkboxStyle" format="reference" /> <!-- Default CheckedTextView style. --> <attr name="checkedTextViewStyle" format="reference" /> <!-- Default ListView style for drop downs. --> <attr name="dropDownListViewStyle" format="reference" /> <!-- Default EditText style. --> <attr name="editTextStyle" format="reference" /> <!-- Default ExpandableListView style. --> <attr name="expandableListViewStyle" format="reference" /> <!-- ExpandableListView with white background. --> <attr name="expandableListViewWhiteStyle" format="reference" /> <!-- Default Gallery style. --> <attr name="galleryStyle" format="reference" /> <!-- Default GestureOverlayView style. --> <attr name="gestureOverlayViewStyle" format="reference" /> <!-- Default GridView style. --> <attr name="gridViewStyle" format="reference" /> <!-- The style resource to use for an ImageButton. --> <attr name="imageButtonStyle" format="reference" /> <!-- The style resource to use for an ImageButton that is an image well. --> <attr name="imageWellStyle" format="reference" /> <!-- Default ListView style. --> <attr name="listViewStyle" format="reference" />

    至于它后面跟的数值Widget.Funui.CompoundButton.CheckBox,如下

     <style name="Widget.CompoundButton.CheckBox">
            <item name="android:button">?android:attr/listChoiceIndicatorMultiple</item>
        </style>

    我们可以看到,它其实就是一个checkbox 中button属性的设置

    下面我们来查找另外的一个属性看看结果<item name="searchViewTextColor">@color/searchview_text_color</item>

    仍然是在res/values/attr.xml里面看到它的身影

    <attr name="searchViewTextColor" format="reference|color" />

    另外,并没有在代码中看到它,只是在一些xml中看到了,如下

     <view class="android.widget.SearchView$SearchAutoComplete"
                    android:id="@+id/search_src_text"
                    android:layout_height="36dip"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:minWidth="@dimen/search_view_text_min_width"
                    android:layout_gravity="center_vertical"
                    android:paddingStart="@dimen/dropdownitem_text_padding_left"
                    android:paddingEnd="@dimen/dropdownitem_text_padding_right"
                    android:singleLine="true"
                    android:ellipsize="end"
                    android:background="@null"
                    android:inputType="text|textAutoComplete|textNoSuggestions"
                    android:textColor="?android:attr/searchViewTextColor"
                    android:textColorHint="?android:attr/searchViewTextColorHint"
                    android:imeOptions="actionSearch"
                    android:dropDownHeight="wrap_content"
                    android:dropDownAnchor="@id/search_edit_frame"
                    android:dropDownVerticalOffset="0dip"
                    android:dropDownHorizontalOffset="0dip"
                    android:contentDescription="@string/searchview_description_query"
                />
    android:textColor="?android:attr/searchViewTextColor"
    它是引用了
    searchViewTextColor 这个值在theme有了定义,相当于引用了@color/searchview_text_color

    这里我们是不是可以得出一个结论呢,
    theme下定义的资源,我们都是可以直接使用的,也就是它就是一个资源的别名,就像定义在value中一样。
    不同的是,它是可以被覆盖的,也就是说,当前应用会覆盖系统默认的
  • 相关阅读:
    Java核心(七):this和super的区别
    Java核心(六):==和equals()的区别;重写equals()方法
    java核心(五):堆内存、栈内存;String赋值时,内存变化
    Java核心(四):Java中的装箱和拆箱
    Java核心(三):代码块的作用
    从数据库中导出.csv文件
    mongodb中数据类型的坑
    return 和 echo 的小坑
    对数据库中初始的数据在后台进行翻译
    SQL语句执行效率及分析
  • 原文地址:https://www.cnblogs.com/zhangshuli-1989/p/zhangshuli_theme_150212181.html
Copyright © 2011-2022 走看看