自定义控件属性

一.layout属性

1.首先在res/values/attrs.xml文件中,定义declare-styleable

<declare-styleable name="RoundTopLayout">
    <attr name="backcolor" format="color"/>
    <attr name="linecolor" format="color"/>
    <attr name="linesize" format="dimension"/>
    <attr name="gapsize" format="dimension"/>
    <attr name="centerwidth" format="dimension"/>
</declare-styleable>

2.在java文件中获取属性值

final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundTopLayout, defStyle, 0);
GapSize = a.getDimension(R.styleable.RoundTopLayout_gapsize, GapSize);
MidWidth = a.getDimension(R.styleable.RoundTopLayout_centerwidth, MidWidth);
Paint_Diameter = a.getDimension(R.styleable.RoundTopLayout_linesize, Paint_Diameter);
LineColor = a.getColor(R.styleable.RoundTopLayout_linecolor, LineColor);
BackColor = a.getColor(R.styleable.RoundTopLayout_backcolor, BackColor);
a.recycle();

3.布局文件自定义命名空间,属性赋值

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:rtl="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
<com.danielhan.sample.library.RoundTopLayout
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:layout_alignParentBottom="true"
        android:layerType="software"
        rtl:centerwidth="70dp"
        rtl:gapsize="20dp"
        rtl:linecolor="@color/gray_999"
        rtl:linesize="1dp">

二.主题style(方便整个项目用一套属性,不必每次用控件都要copy)

1.首先在res/values/attrs.xml文件中,定义attr

<attr name="roundTopLayoutStyle" format="reference"/>

2.然后再res/values/styles中添加全局属性style,然后引入到项目所用theme中

<style name="CustomRoundTopLayoutStyle">
    <item name="backcolor">#ff0000</item>
    <item name="linecolor">#00ff00</item>
    <item name="linesize">10dp</item>
    <item name="gapsize">40dp</item>
    <item name="centerwidth">150dp</item>
</style>
<style name="deyangStyle" parent="AppBaseTheme">
    <item name="tab_supplier_string">@string/tab_supplier_dy</item>
    <item name="tab_home">@drawable/tab_home_normal</item>
    <item name="tab_supplier">@drawable/tab_supplier_normal</item>
    <item name="tab_activity">@drawable/expb_show</item>
    <item name="tab_demand">@drawable/tab_demand_normal_dy</item>
    <item name="tab_my">@drawable/tab_my_normal</item>
    <item name="tab_font_color">@drawable/tab_font_color_dy</item>
    <item name="roundTopLayoutStyle">@style/CustomRoundTopLayoutStyle</item>
</style>

AndroidManifest.xml 如下,用的是deyangStyle

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/deyangStyle">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

3.java中使用R.attr.roundTopLayoutStyle

if (attrs != null) {
    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundTopLayout, R.attr.roundTopLayoutStyle, 0);
    GapSize = a.getDimension(R.styleable.RoundTopLayout_gapsize, GapSize);
    MidWidth = a.getDimension(R.styleable.RoundTopLayout_centerwidth, MidWidth);
    Paint_Diameter = a.getDimension(R.styleable.RoundTopLayout_linesize, Paint_Diameter);
    LineColor = a.getColor(R.styleable.RoundTopLayout_linecolor, LineColor);
    BackColor = a.getColor(R.styleable.RoundTopLayout_backcolor, BackColor);
    a.recycle();
}

三.layout style

1.res/values/styles中添加style

<style name="CustomRoundTopLayoutStyle1">
    <item name="linecolor">#00ff00</item>
    <item name="linesize">10dp</item>
    <item name="gapsize">40dp</item>
    <item name="centerwidth">150dp</item>
</style>

2.layout 中使用style

<com.danielhan.sample.library.RoundTopLayout
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:layout_alignParentBottom="true"
    android:layerType="software"
    style="@style/CustomRoundTopLayoutStyle1">

优先级顺序

  1. 首先获取给定的AttributeSet中的属性值
  2. 如果找不到,则去AttributeSet中style(你在写布局文件时定义的style=”@style/xxxx”)指定的资源获取
  3. 如果找不到,则去defStyleAttr以及defStyleRes中的默认style中获取
  4. 最后去找的是当前theme下的基础值

四.提供set和get方法

public void setLineColor(int lineColor) {
    this.lineColor = lineColor;
    paint.setColor(lineColor);
    invalidate();
}

总结,完善的自定义控件,必须提供这四种属性设置方式

代码传送门

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值