Android 自定义控件 思路

自定义组合控件


建立一个类继承某种layout,有三个构造函数,mymulview(Context context),代码创建组件时候调用

mymulview(Context context, AttributeSet attrs),用xml创建组建时候调用。 AttributeSet attrs,包含xml中定义的各项属性值。

在重写这个构造时候,要实例化一个view组合(通过inflate一个xml布局文件)。这个实例化的view就是自定义view。


自定义属性,要在values中建立xml文件。这个是用来在xml布局中,定义属性值,在通过xml构造组件时候mymulview(Context context, AttributeSet attrs),attrs包含属性值集合

构造函数中,提取其中包含的属性值,然后给控件设置

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


xml布局文件中,名字空间声明。    xmlns:test="http://schemas.android.com/apk/res/com.example.myview"

test的名称可以随便设置,用在自定义属性的前面,后面不能随便http://schemas.android.com/apk/res/+所在的报名


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:test="http://schemas.android.com/apk/res/com.example.myview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.example.myview.mymulview
        android:id="@+id/myview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        test:mytext="zhedouxing" >
    </com.example.myview.mymulview>

</LinearLayout>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值