组合控件1—— 设置框

之前的自定义控件——初识自定义控件
我们了解到了自定义控件分为三种,自制控件,组合控件,拓展控件。

而我们在
自制控件1 开关按钮
自制控件2 —— 自制控件 仿qq侧滑菜单
也已经针对自制控件写了几个小demo
现在,让我们开始进行自定义控件的第二种,组合控件。

组合控件常常和自定义属性结合在一起,在本文的后半部分,我们也会涉及到自定义属性。

正式开始
假设我们有很多个这样的设置框,

样式.png

组合控件其实可以分为这么大概几个步骤
1、新建一个类,比如叫做JmLayout,继承自RelativeLayout或者LinearLayout等。(反正都是继承自ViewGroup的子类,因为组合嘛,里面要放控件)
2、写一个xml布局文件,这里就是我们想要组合的控件。
3、在JmLayout里面的构造方法将xml填充进去
4、利用JmLayout的全路径名就可以成功使用这个组合控件。
至此组合控件完成,

一、组合几个控件成为一个新的控件

1、新建一个类,比如叫做JmLayout,继承自RelativeLayout或者LinearLayout等。(反正都是继承自ViewGroup的子类,因为组合嘛,里面要放控件)

public class JmLayout extends RelativeLayout{


    private View view;

    public JmLayout(Context context) {
        super(context);

    }

    public JmLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

}

2、写一个xml布局文件,这里就是我们想要组合的控件。

item_my_style_view

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#88877b7b"
    >

    <TextView
        android:id="@+id/mTvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选项名称"
        android:textSize="20dp"
        android:textColor="#ff0000"
        android:layout_margin="10dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="备注"
        android:textSize="18dp"
        android:layout_below="@id/mTvName"
        />

    <ImageView
        android:id="@+id/mIv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10sp"
        />
</RelativeLayout>

3、在JmLayout里面的构造方法将xml填充进去
JmLayout

注意 View.inflate方法的第三个参数在这里我们写的是JmLayout.this

public class JmLayout extends RelativeLayout{
    private View view;

    public JmLayout(Context context) {
        super(context);
        initView();
    }

    public JmLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {

        //  View.inflate  View的填充器
        //  最后一个参数表示把谁当做父容器,这里我们当MySettingView当做父容器
        //  当然,如果这里写null就什么都没有显示了
        view = View.inflate(getContext(),R.layout.item_my_style_view, JmLayout.this);
    }
}

4、利用JmLayout的全路径名就可以成功使用这个组合控件。

main_activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.amqr.combinationview.MainActivity">

    <com.amqr.combinationview.JmLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         >
    </com.amqr.combinationview.JmLayout>
</RelativeLayout>

MainActivity 当前那里都不需要动。

//本Demo是为演示   组合控件

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
}

运行效果图:

2016-03-06_19-10-18.png

二、自定义属性

经过上过上面的步骤,可能有人会说,我直接include不就好,何必这么麻烦。组合控件的当然不止只有这点用处。

下面就结合来演示下 组合控件+自定义属性

什么是属性?就比如TextView来说,text是属性,layout_width是属性,textSize也是属性,但是这些都是系统给我们的。我们这里来自定义一下属性。

1,在values文件夹下新建一个名为 attrs 的资源文件(名字一定得是 attrs)

根节点是resources
declare-styleable节点的name是指明适用于哪一个 控件
name 是自定义属性的名称,format是自定义属性的输入类型

<?xml version="1.0" encoding="utf-8"?>
<resources>
    // 这个自定义属性适用于哪个控件
    <declare-styleable name="JmLayout">
        // name 自定义属性的名称   format 自定属性的输入类型
        <attr name="kind" format="string"/>

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


</resources>

2、引用到当前控件的布局文件根节点添加命名空间,使用自定义属性

  • 先添加命名空间

eclipse下的命名空间命名规则:
xmlns:命名空间=”http://schemas.android.com/apk/res/程序包名”

android studio下的命名空间命名规则:
xmlns:命名控件=”http://schemas.android.com/apk/res-auto”

  • 后在控件里通过 命名空间名:自定义属性名=”属性值” 的方式使用自定义属性

大概如下使用

<?xml version="1.0" encoding="utf-8"?>
    <!--xmlns:命名空间="http://schemas.android.com/apk/res/程序包名"-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:amqr="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.amqr.combinationview.MainActivity">

    <com.amqr.combinationview.JmLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        amqr:kind="皮肤"
         >
    </com.amqr.combinationview.JmLayout>
</RelativeLayout>

3、在自定义控件里面对自定义属性进行相关的操作(可选)

public class JmLayout extends RelativeLayout{


    private View view;
    private TextView mTvKind;
    private TextView mTvDesc;

    public JmLayout(Context context) {
        super(context);
        initView();
    }

    public JmLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();

        // 得到自定义输入的值
        String kind = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "kind");
        mTvKind.setText(kind);
    }

    private void initView() {

        //  View.inflate  View的填充器
        //  最后一个参数表示把谁当做父容器,这里我们当MySettingView当做父容器
        //  当然,如果这里写null就什么都没有显示了
        view = View.inflate(getContext(),R.layout.item_my_style_view, JmLayout.this);

        mTvKind = (TextView) view.findViewById(R.id.mTvKind);
        mTvDesc = (TextView) view.findViewById(R.id.mTvDesc);
    }
}

最终运行效果

2016-03-06_20-15-11.png

在上面的代码中,我们还有一个自定义属性desc没有使用到,但是使用方式相同。

通过自定义控件+自定义属性的方式,我们可以一次造轮子,多次方便地使用,减少冗余代码,提高开发效率。

关于组合控件的设置框的博文到此结束

自制控件2 —— 自制控件 仿qq侧滑菜单中,将继续进行组合控件的demo编写。

本文完。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值