Android自定义控件使用attrs属性

最近一直在忙项目,只好周末整理一下代码。自己做笔记的同时与大家一起学习交流。

一、attrs.xml文件的中属性类型format值的格式

"reference" //引用
"color" //颜色
"boolean" //布尔值
"dimension" //尺寸值
"float" //浮点值
"integer" //整型值
"string" //字符串
"fraction" //百分数,比如200%

枚举型的格式:

< attr name="orientation">
  < enum name="horizontal" value="0" />
  < enum name="vertical" value="1" />
< /attr>

XML文件中使用:

android:orientation = "vertical"

标志位、位或运算,格式如下:

< attr name="windowSoftInputMode">
  < flag name = "stateUnspecified" value = "0" />
  < flag name = "stateUnchanged" value = "1" />
  < flag name = "stateHidden" value = "2" />
  < flag name = "stateAlwaysHidden" value = "3" />
  < flag name = "stateVisible" value = "4" />
  < flag name = "stateAlwaysVisible" value = "5" />
  < flag name = "adjustUnspecified" value = "0x00" />
  < flag name = "adjustResize" value = "0x10" />
  < flag name = "adjustPan" value = "0x20" />
  < flag name = "adjustNothing" value = "0x30" />
< /attr>

XML文件中使用:

android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">

属性定义可以指定多种类型:

< attr name = "background" format = "reference|color" />

XML文件中使用:

android:background = "@drawable/图片ID|#00FF00"

以上内容转自http://blog.csdn.net/wxg630815/article/details/6989316

二、自定义控件的使用

1、自定义控件在布局中使用,只需要写入类名就可以了。其他属性和一般控件写法一样。如:android:layout_width=”wrap_content”等。

<com.chengkni.demo.attrs.MyView      xmlns:test="http://schemas.android.com/apk/res/com.chengkni.demo.attrs"
  android:id="@+id/view_main_my_view"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  test:tText="hello word" />

2、如果自定义的控件想使用自己定义的属性标签。在布局中添加命名空间“xmlns:‘名字’=”http://schemas.android.com/apk/res/‘自己软件包名’“。然后在工程中values文件下添加attrs.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomView">
        <attr name="tText" format="string" />
    </declare-styleable>
</resources>

添加完成后只需在布局中写出标签的内容即可。如上边布局文件,xmlns:test=”http://schemas.android.com/apk/res/com.chengkni.demo.attrs”就可以使用test这个命名空间,然后通过命名空间使用attrs.xml下的tText标签。

3、我们定义的自定义的控件MyView,继承了View。控件有个字段mViewName用来标记改控件的名字。我们可以通过在布局文件设置自定义属性就可以在实现给MyView的mViewName命名。

package com.chengkni.demo.attrs;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MyView extends View {
    private String TAG = this.getClass().getSimpleName();
    private String mViewName;

    public MyView(Context context) {
        super(context);
        Log.e(TAG, "---------- MyView(1) ----------");
        init(context, null);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.e(TAG, "---------- MyView(2) ----------");
        init(context, attrs);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Log.e(TAG, "---------- MyView(3) ----------");
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs == null) {
            mViewName = "Attrs is null";
        } else {
            TypedArray array = context.obtainStyledAttributes(attrs,
                    R.styleable.CustomView);
            mViewName = array.getString(R.styleable.CustomView_tText);
            array.recycle();
        }
        Log.e(TAG, "mString = " + mViewName);
    }

    public String getmViewName() {
        return mViewName;
    }

    public void setmViewName(String mViewName) {
        this.mViewName = mViewName;
    }
}

完整工程“Android自定义控件使用attrs属性”下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值