Android_UI_res

Android 之 ?attr/

2018-09-04  本文已影响346人  PuHJ

在代码中遇到了android:tint="?attr/iconColor", iconColor这个是什么引用,在哪定义赋值的,真是不明觉厉!后来发?attr 是和主题Theme有关。也就是说在Resource资源中定义,在主题Theme中赋值,使用的时候,该值会随着主题的变化而获取的值也是不同的


一、自定义属性att基本用法

(1)、定义

在values文件夹中创建一个attrs_base.xml,名称随意起。定义一个属性,并给出属性的格式。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="toolbarTitleColor" format="color" />
    <attr name="toolbarTitleSize" format="dimension" />
</resources>

(2)、赋值

在Theme中赋值,但是要在AndroidManifest.xml中使用该Theme才能生效,否则会报错

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">?attr/toolbarTitleColor</item>
        
        <item name="toolbarTitleColor">@color/colorAccent</item>
        <item name="toolbarTitleSize">30sp</item>

</resources>

(3)、使用

定义一个style,在style中使用自定义的属性attr

    <style name="TextStyle" parent="@style/TextAppearance.AppCompat.Title">
        <item name="android:textSize">?attr/toolbarTitleSize</item>
        <item name="android:textColor">?attr/toolbarTitleColor</item>
    </style>
    
    // TextView中使用
    android:textAppearance="@style/TextStyle"

二、Attr、Style、Theme属性优先级

布局xml中的属性text、Style中的属性text、自定义的构造参数defStyleAttr中的text属性和Theme中的定义的text属性,如果都定义使用了同一个属性,他们的优先级的如何?

(1)、 首先定义一个自定义View,SelfView

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="toolbarTitleColor" format="color" />
    <attr name="toolbarTitleSize" format="dimension" />
    <attr name="attr_defStyle" format="reference"/>


    <declare-styleable name="SelfView">

        <attr name="text1" format="string"/>
        <attr name="text2" format="string"/>
        <attr name="text3" format="string"/>
        <attr name="text4" format="string"/>

    </declare-styleable>

</resources>

(2)、 自定义Style

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">?attr/toolbarTitleColor</item>
        
        <item name="toolbarTitleColor">@color/colorAccent</item>
        <item name="toolbarTitleSize">30sp</item>

        <item name="attr_defStyle">@style/style_attr_defStyleAttr</item> // 之前定义的attr,指向下面的Style

        <!--Theme的赋值  SelfView中定义的-->
        <item name="text1">text1 theme</item>
        <item name="text2">text2 theme</item>
        <item name="text3">text3 theme</item>
        <item name="text4">text4 theme</item>
    </style>

    <!--SelfView中的defStyleAttr使用 -->
    <style name="style_attr_defStyleAttr">
        <item name="text1">text1 style_attr_defStyleAttr</item>
        <item name="text2">text2 style_attr_defStyleAttr</item>
        <item name="text3">text3 style_attr_defStyleAttr</item>
    </style>

    <!--@style/style_viewStyle中 -->
    <style name="style_viewStyle">
        <item name="text1">text1 style_viewStyle</item>
        <item name="text2">text2 style_viewStyle</item>
    </style>


    <style name="TextStyle" parent="@style/TextAppearance.AppCompat.Title">
        <item name="android:textSize">?attr/toolbarTitleSize</item>
        <item name="android:textColor">?attr/toolbarTitleColor</item>
    </style>

</resources>

(3)、 自定义SelfView

public class SelfView extends android.support.v7.widget.AppCompatTextView {

    public SelfView(Context context) {
        this(context,null);
    }

    public SelfView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,R.attr.attr_defStyle); // 使用定义的属性
    }

    public SelfView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.SelfView,defStyleAttr,0);
        String text1 = typedArray.getString(R.styleable.SelfView_text1);
        String text2 = typedArray.getString(R.styleable.SelfView_text2);
        String text3 = typedArray.getString(R.styleable.SelfView_text3);
        String text4 = typedArray.getString(R.styleable.SelfView_text4);
        typedArray.recycle();

        Log.e(getClass().getSimpleName(), "SelfView: " +text1 );
        Log.e(getClass().getSimpleName(), "SelfView: " +text2 );
        Log.e(getClass().getSimpleName(), "SelfView: " +text3 );
        Log.e(getClass().getSimpleName(), "SelfView: " +text4 );

    }
}

(4)、 使用

    <com.phj.SelfView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark"
        android:text="Hello World!"
        style="@style/style_viewStyle"
        android:textAppearance="@style/TextStyle"
        app:text1="xml text1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

(5)、 输出

 SelfView: xml text1
 SelfView: text2 style_viewStyle
 SelfView: text3 style_attr_defStyleAttr
 SelfView: text4 theme

小结 :

GitHub代码:https://github.com/puhaojie/AndroidAttrDemo

三、自定义View中format

format 描述 例子
reference 某一个资源的引用 <attr name = "background" format = "reference" />使用 :android:background = "@drawable/back"
color 颜色值 <attr name = "textColor" format = "color" />
boolean 布尔值 <attr name = "focusable" format = "boolean" />
dimension 尺寸大小 <attr name = "layout_width" format = "dimension" /> 使用 : android:layout_width = "1dp"
float 浮点值
integer 整型
string 字符串
fraction 百分数 <attr name = "pivotX" format = "fraction" /> 使用 : android:pivotX = "200%"
enum 枚举值 <attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr>
flag 按位或 <attr name="windowSoftInputMode"> <flag name = "stateUnspecified" value = "0" /> </attr>
上一篇下一篇

猜你喜欢

热点阅读