安卓自定义控件、自定义xml里的属性的用法

   在android相关应用开发过程中,固定的一些属性可能满足不了开发的需求,所以在一些特殊情况下,需要自定义控件与属性,本文将以此问题进行详细介绍,需要的朋友可以参考下。


步骤一: 继承 View 定制 自己需要的 控件


public class State3BidTitleView extends RelativeLayout implements OnClickListener {


实现 时 需要 重写 带两个 参数 的 构造函数 ,即如下, 否则运行时会 报错崩溃

    public State3BidTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(this);


步骤二:xml 布局文件中按如下方式 使用, 其中 xmlns:app= 中的 app可根据自己的需要随意取名,"http://schemas.android.com/apk/res/com.snippet.xsdjr" 中的 后面的com.snippet.xsdjr 是app的 包名 , 前面的不变

     <com.snippet.xsdjr.ui.view.State3BidTitleView  xmlns:app="http://schemas.android.com/apk/res/com.snippet.xsdjr"
             android:id="@+id/amount3StateTV1"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             app:bidTitleText="测试"
             android:layout_weight="1"/>


步骤三:上面两步完成后基本可以使用了,但是使用过程会有些不方便,因为只能使用系统继承过来的 属性,比如 android:layout_width=  、   android:id=  等等之类,

     像上面例子中 的 app:bidTitleText="测试" 是怎么来的呢? 用法如下:

   在 res/ values / attrs.xml  写 如下 定义:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="BidTitleAttrs">
        <attr name="bidTitleText" format="string" />
    </declare-styleable>

</resources>

其中 BidTitleAttrs 可根据 自己的 view的意义 自己 取名, 这个名字是 在 (Context context, AttributeSet attrs)  构造函数中使用的,bidTitleText 即是在 xml 布局文件中使用,但是命名空间 需要 自己 定制 ,不能使用 android: 作为 命名空间 ,使用方式 如右:       app:bidTitleText="测试"  


步骤四: 在 两参数 的 构造函数 中 需要 来 解析  attrs.xml  中 定义 的 定制样式,解析方式如下:

注意 : R.styleable.BidTitleAttrs 中的 BidTitleAttrs 是 的 attrs.xml 中定义的 ,  获取 其中一条属性时 使用 a.getString(R.styleable.BidTitleAttrs_bidTitleText); 其中 BidTitleAttrs_bidTitleText 的 名字 是由 BidTitleAttrs + "_" + bidTitleText 拼接起来的;

另外一个需要注意的 就是 不同值类型的 属性 需要 使用不同的 获取 方法 getString   ,  getDimon  ,  getBool  之类的 看名字慢慢找


    public State3BidTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(this);
        
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BidTitleAttrs);
        titleText = a.getString(R.styleable.BidTitleAttrs_bidTitleText);
        a.recycle();

        
        this.tabTextView = new TextView(context);
        this.tabTextView.setText(this.titleText);
        this.tabTextView.setTextSize(14.0F);
        this.tabTextView.setTextColor(getResources().getColor(R.color.bid_red_dark));
        RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp1.addRule(RelativeLayout.CENTER_IN_PARENT);
        this.tabTextView.setLayoutParams(lp1);
        this.tabTextView.setId(R.id.finance_title);
        addView(this.tabTextView);

}


步骤五:attr.xml 是 属性值 的 类型枚举值 如下:

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

       (1)属性定义:

            <declare-styleable name = "名称">

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

           </declare-styleable>

       (2)属性使用:

            <ImageView

                  android:layout_width = "42dip"

                  android:layout_height = "42dip"

                  android:background = "@drawable/图片ID"

            />

2. color:颜色值


<declare-styleable name = "名称">

<attr name = "textColor" format = "color" />

</declare-styleable>

3. boolean:布尔值


<declare-styleable name = "名称">

<attr name = "focusable" format = "boolean" />

</declare-styleable>

4. dimension:尺寸值。


<declare-styleable name = "名称">

<attr name = "layout_width" format = "dimension" />

</declare-styleable>

5. float:浮点值。

6. integer:整型值。

7. string:字符串

8. fraction:百分数。

9. enum:枚举值

10. flag:位或运算


注意:

属性定义时可以指定多种类型值。

(1)属性定义:

<declare-styleable name = "名称">

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

</declare-styleable>

(2)属性使用:

<ImageView

android:layout_width = "42dip"

android:layout_height = "42dip"

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

/>







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值