android attr 定义,Android 之 ?attr/

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

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

(1)、定义

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

(2)、赋值

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

@color/colorPrimary

@color/colorPrimaryDark

?attr/toolbarTitleColor

@color/colorAccent

30sp

(3)、使用

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

?attr/toolbarTitleSize

?attr/toolbarTitleColor

// TextView中使用

android:textAppearance="@style/TextStyle"

小结 : 自定义的属性就是这样。使用系统定义好的?android:attr/ 或者 ?android:

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

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

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

(2)、 自定义Style

@color/colorPrimary

@color/colorPrimaryDark

?attr/toolbarTitleColor

@color/colorAccent

30sp

@style/style_attr_defStyleAttr // 之前定义的attr,指向下面的Style

text1 theme

text2 theme

text3 theme

text4 theme

text1 style_attr_defStyleAttr

text2 style_attr_defStyleAttr

text3 style_attr_defStyleAttr

text1 style_viewStyle

text2 style_viewStyle

?attr/toolbarTitleSize

?attr/toolbarTitleColor

(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)、 使用

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

format

描述

例子

reference

某一个资源的引用

使用 :android:background = "@drawable/back"

color

颜色值

boolean

布尔值

dimension

尺寸大小

使用 : android:layout_width = "1dp"

float

浮点值

integer

整型

string

字符串

fraction

百分数

使用 : android:pivotX = "200%"

enum

枚举值

flag

按位或

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值