Android自定义布局属性

需要在values下定义attrs.xml


<declare-styleable name="myStyleable">
        <attr name="max" format="integer"/>
        <attr name="progress" format="integer"/>
        <attr name="textSize" format="dimension"/>
        <attr name="smallBgColor" format="color"/>
        <attr name="progressColor" format="color"/>
    </declare-styleable>
其中fomat表示属性的取值类型


integer:整数
dimension:尺寸 dp dip sp px
color:颜色
string:字符串
float:浮点数 android:rating="1.5"
referrence:引用 @id/btn @drawable/pic
解析属性


public XykjProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //解析自定义属性
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myStyleable);
        textSize = ta.getDimensionPixelSize(R.styleable.myStyleable_textSize, 0);
        max = ta.getInteger(R.styleable.myStyleable_max,100);
        progress = ta.getInteger(R.styleable.myStyleable_progress,0);
        smallBgColor = ta.getColor(R.styleable.myStyleable_smallBgColor,0xff3F51B5);
        progressColor = ta.getColor(R.styleable.myStyleable_progressColor,0xffFF4081);
        //释放资源
        ta.recycle();
    }
使用属性


需要在布局中声明前缀的命名空间
xmlns:app="http://schemas.android.com/apk/res-auto"


<com.xykj.myprogressbar.XykjProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:textSize="30sp"
        app:smallBgColor="#ff000000"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值