GroupView 组合控件

来源:互联网 发布:淘宝店需要营业执照 编辑:程序博客网 时间:2024/05/19 02:02

这里写图片描述

../values/attr.xml 配置属性

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="LevelMenuItemView">        <attr name="text" format="string"></attr>        <attr name="text_size" format="dimension"></attr>        <attr name="text_color" format="color"></attr>        <attr name="img_src" format="reference"></attr>        <attr name="img_width" format="dimension"></attr>        <attr name="img_heigh" format="dimension"></attr>    </declare-styleable></resources>

../view/GroupView.java

public class LevelMenuItemView extends LinearLayout {    public LevelMenuItemView(Context context) {        super(context, null);    }    public LevelMenuItemView(Context context, AttributeSet attrs) {        super(context, attrs);        LayoutInflater layoutInflater = (LayoutInflater) context.                getSystemService(Context.LAYOUT_INFLATER_SERVICE);        layoutInflater.inflate(R.layout.level_menu_item, this);        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LevelMenuItemView);        initView(typedArray);    } }

初始化控件,配置属性:

    ImageView img_top;    TextView tv_title;    private void initView(TypedArray typedArray) {        tv_title = (TextView) findViewById(R.id.tv_title);        tv_title.setText(typedArray.getString(R.styleable.LevelMenuItemView_text));        tv_title.setTextSize(typedArray.getDimension(R.styleable.LevelMenuItemView_text_size, 12));        tv_title.setTextColor(typedArray.getColor(R.styleable.LevelMenuItemView_text_color, 0xffffffff));        img_top = (ImageView) findViewById(R.id.img_top);        img_top.setImageResource(typedArray.getResourceId(R.styleable.LevelMenuItemView_img_src, 0));        int width = (int) typedArray.getDimension(R.styleable.LevelMenuItemView_img_width, 25);        int heigh = (int) typedArray.getDimension(R.styleable.LevelMenuItemView_img_heigh, 25);        LayoutParams params = new LayoutParams(width, heigh);        img_top.setLayoutParams(params);        typedArray.recycle();    }

绑定点击事件:

    public void setImageOnclick(final OnItemImageClick onclick) {        img_top.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                onclick.click(v);            }        });    }    public interface OnItemImageClick {        public void click(View view);    }

.main.xml 文件中使用
< 重点: 添加该自定义控件的命名空间 >

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:linc="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#eee"    android:orientation="horizontal"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin">    <com.mjc.slidebackdemo.android.view.demo.groupView.LevelMenuItemView        android:id="@+id/item1"        android:layout_width="70dp"        android:layout_height="80dp"        linc:img_heigh="48dp"        linc:img_src="@mipmap/xiaoshidai"        linc:img_width="48dp"        linc:text="item1"        linc:text_color="#80fa8072"        linc:text_size="12sp" />    <com.mjc.slidebackdemo.android.view.demo.groupView.LevelMenuItemView        android:id="@+id/item2"        android:layout_width="70dp"        android:layout_height="80dp"        linc:img_heigh="48dp"        linc:img_src="@mipmap/xiangjiankang"        linc:img_width="48dp"        linc:text="item2"        linc:text_color="#80fa8072"        linc:text_size="12sp" />    <com.mjc.slidebackdemo.android.view.demo.groupView.LevelMenuItemView        android:id="@+id/item3"        android:layout_width="70dp"        android:layout_height="80dp"        linc:img_heigh="48dp"        linc:img_src="@mipmap/zhidemai"        linc:img_width="48dp"        linc:text="item3"        linc:text_color="#80fa8072"        linc:text_size="12sp" /></LinearLayout>
0 0
原创粉丝点击