Android 之 ?attr/ 用法
?attr 是和主题Theme有关,也就是说在Resource资源中定义,在主题Theme中赋值,使用的时候,该值会随着主题的变化而获取的值也是不同的。
一、自定义属性att基本用法
(1)、定义
在values文件夹中创建一个attrs_base.xml,名称随意起。定义一个属性,并给出属性的格式。
<resources>
<attr name="playBarBackground" format="reference" />
</resources>
//reference 是某一个资源的引用,drawable内定义的xml就属于资源。
(2)、赋值
在Theme中赋值,但是要在AndroidManifest.xml中使用该Theme才能生效,否则会报错
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/red</item>
<item name="colorPrimaryDark">@color/red</item>
<item name="colorAccent">@color/red</item>
<item name="playBarBackground">@drawable/play_bar_bg_selector</item>
</style>
<style name="AppTheme" parent="AppBaseTheme" />
</resources>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
3)、使用
定义一个style,在style中使用自定义的属性attr
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fl_play_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/play_bar_height"
android:background="?attr/playBarBackground">
</FrameLayout>
小结 : 自定义的属性就是这样。使用系统定义好的?android:attr/ 或者 ?android:
二、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>
<[email protected]/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
小结 :
直接在xml中使用的优先
在xml中 style="@style/style_viewStyle"次之
自定义SelfView中使用的defStyleAttr再次之
最后是Theme中的赋值的属性
三、自定义View中format
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/red</item>
<item name="colorPrimaryDark">@color/red</item>
<item name="colorAccent">@color/red</item>
<item name="playBarBackground">@drawable/play_bar_bg_selector</item>
</style>
<style name="AppBaseThemeDark" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/grey_900</item>
<item name="colorPrimaryDark">@color/grey_900</item>
<item name="colorAccent">@color/grey_900</item>
<item name="playBarBackground">@drawable/play_bar_bg_selector_dark</item>
</style>
<attr name="playBarBackground" format="reference" />
<style name="AppTheme" parent="AppBaseTheme" />
<style name="AppThemeDark" parent="AppBaseThemeDark" />
<style name="AppTheme.Search" parent="AppBaseTheme">
<item name="colorAccent">@color/white</item>
</style>
<style name="AppThemeDark.Search" parent="AppBaseThemeDark">
<item name="colorAccent">@color/white</item>
</style>
<style name="AppTheme.Text" parent="TextAppearance.AppCompat" />
</resources>