Android中关于自定义属性的理解

自定义属性步骤:

  1. 自定义一个CustomView(extends View )类,也可以继承现有的类,比如LinearLayout或者ImageView
  2. 编写values/attrs.xml,在其中编写styleable和item等标签元素
  3. 在布局文件中CustomView使用自定义的属性(注意namespace)
  4. 在CustomView的构造方法中通过TypedArray获取


开始编写一个自定义类。

public class MyView extends LinearLayout{
    private ImageView imageView;
    private TextView textView;
    private CharSequence text;
    private Drawable drawable;
    private float textSize;
    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context,attrs);

    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context,attrs);
    }

    private void initView(Context context, @Nullable AttributeSet attrs){
        //注意这个地方R.styleable.MyView中styleable必须是它,后面跟的是自定义控件的名字
        //或者是values/attrs中自定义属性的name
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);
        //values/attrs中MyView下面的text,使用MyView_text中方式获取,注意下划线
        text = array.getText(R.styleable.MyView_text);
        if(text == null){
            text = "";
        }
        Drawable d = array.getDrawable(R.styleable.MyView_src);
        if(d != null){
            drawable = d;
        }else {
            throw new RuntimeException("图像资源为空");
        }

        textSize = array.getDimension(R.styleable.MyView_textSize,12);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.image_button_layout,this);
        imageView = (ImageView) findViewById(R.id.icon_part);
        imageView.setImageDrawable(drawable);

        textView = (TextView) findViewById(R.id.text_part);
        textView.setTextSize((float) textSize);
        textView.setText(text);
        if(text.equals("")||text==null){
            textView.setVisibility(View.GONE);
        }
        array.recycle();
    }

}

编写values/attrs.xml,在其中编写styleable和item等标签元素

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView">
        <attr name="text" format="reference"/>
        <attr name="src" format="reference"/>
        <attr name="textSize"  format="dimension"/>
    </declare-styleable>
</resources>

使用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cxd="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.xuedongchao.myattrs.activity.MainActivity">

    <com.example.xuedongchao.myattrs.view.MyView
        android:id="@+id/my_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        cxd:src="@mipmap/ic_launcher"
        cxd:text="@string/test"
        cxd:textSize="14sp"/>

</RelativeLayout>

注意:xmlns:cxd="http://schemas.android.com/apk/res-auto"   否则不能使用自定义属性


自定义属性值类型

1. reference:参考某一资源ID。

(1)属性定义:

  1. <declare-styleable name="名称">
  2. <attr format="reference" name="background" />
  3. </declare-styleable>

(2)属性使用:

 
 
  1. <ImageView
  2. android:layout_width="42dip"
  3. android:layout_height="42dip"
  4. android:background="@drawable/图片ID" />

2. color:颜色值。

(1)属性定义:

 
 
  1. <declare-styleable name="名称">
  2. <attr format="color" name="textColor" />
  3. </declare-styleable>

(2)属性使用:

 
 
  1. <TextView
  2. android:layout_width="42dip"
  3. android:layout_height="42dip"
  4. android:textColor="#00FF00" />

3. boolean:布尔值。

(1)属性定义:

 
 
  1. <declare-styleable name="名称">
  2. <attr format="boolean" name="focusable" />
  3. </declare-styleable>

(2)属性使用:

 
 
  1. <Button
  2. android:layout_width="42dip"
  3. android:layout_height="42dip"
  4. android:focusable="true" />

4. dimension:尺寸值。

(1)属性定义:

 
 
  1. <declare-styleable name="名称">
  2. <attr format="dimension" name="layout_width" />
  3. </declare-styleable>

(2)属性使用:

 
 
  1. <Button
  2. android:layout_width="42dip"
  3. android:layout_height="42dip" />

5. float:浮点值。

(1)属性定义:

 
 
  1. <declare-styleable name="AlphaAnimation">
  2. <attr format="float" name="fromAlpha" />
  3. <attr format="float" name="toAlpha" />
  4. </declare-styleable>

(2)属性使用:

 
 
  1. <alpha
  2. android:fromAlpha="1.0"
  3. android:toAlpha="0.7" />

6. integer:整型值。

(1)属性定义:

 
 
  1. <declare-styleable name="AnimatedRotateDrawable">
  2. <attr format="integer" name="frameDuration" />
  3. <attr format="integer" name="framesCount" />
  4. </declare-styleable>

(2)属性使用:

 
 
  1. <animated-rotate
  2. android:frameDuration="100"
  3. android:framesCount="12"
  4. />

7. string:字符串。

(1)属性定义:

 
 
  1. <declare-styleable name="MapView">
  2. <attr format="string" name="apiKey" />
  3. </declare-styleable>

(2)属性使用:

 
 
  1. <com.google.android.maps.MapView
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" />

8. fraction:百分数。

(1)属性定义:

 
 
  1. <declare-styleable name="RotateDrawable">
  2. <attr format="fraction" name="pivotX" />
  3. <attr format="fraction" name="pivotY" />
  4. </declare-styleable>

(2)属性使用:

 
 
  1. <rotate
  2. android:pivotX="200%"
  3. android:pivotY="300%"
  4. />

9. enum:枚举值。

(1)属性定义:

 
 
  1. <declare-styleable name="名称">
  2. <attr name="orientation">
  3. <enum name="horizontal" value="0" />
  4. <enum name="vertical" value="1" />
  5. </attr>
  6. </declare-styleable>

(2)属性使用:

 
 
  1. <LinearLayout
  2. android:orientation="vertical" >
  3. </LinearLayout>

10. flag:位或运算。

(1)属性定义:

 
 
  1. <declare-styleable name="名称">
  2. <attr name="windowSoftInputMode">
  3. <flag name="stateUnspecified" value="0" />
  4. <flag name="stateUnchanged" value="1" />
  5. <flag name="stateHidden" value="2" />
  6. <flag name="stateAlwaysHidden" value="3" />
  7. <flag name="stateVisible" value="4" />
  8. <flag name="stateAlwaysVisible" value="5" />
  9. <flag name="adjustUnspecified" value="0x00" />
  10. <flag name="adjustResize" value="0x10" />
  11. <flag name="adjustPan" value="0x20" />
  12. <flag name="adjustNothing" value="0x30" />
  13. </attr>
  14. </declare-styleable>

(2)属性使用:

 
 
  1. <activity
  2. android:windowSoftInputMode="stateUnspecified | stateUnchanged | stateHidden" >
  3. </activity>

注意:属性定义时可以指定多种类型值:

(1)属性定义:

 
 
  1. <declare-styleable name="名称">
  2. <attr format="reference|color" name="background" />
  3. </declare-styleable>

(2)属性使用:

 
 
  1. <ImageView
  2. android:layout_width="42dip"
  3. android:layout_height="42dip"
  4. android:background="@drawable/图片ID|#00FF00" />



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值