美文网首页android技术
自定义组合控件的属性复用

自定义组合控件的属性复用

作者: 最黑暗的自己 | 来源:发表于2018-07-29 15:57 被阅读1次
    1、自定义layout
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        android:background="@color/colorPrimary"
        android:orientation="horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="50dp">
    <Button
        android:background="@null"
        android:layout_marginLeft="10dp"
        android:layout_alignParentLeft="true"
        android:id="@+id/bt_back"
        android:text="返回"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/bar_title"
            android:textSize="20dp"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:text="2222222"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:background="@null"
            android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"
            android:id="@+id/bt_in"
            android:text="前进"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </RelativeLayout>
    
    2、自定义View
    public class MyTitleBar extends RelativeLayout {
    
    
        private String mMyBarTitle;
        private int mMyBarSize;
    
        public MyTitleBar(Context context) {
           this(context,null);
        }
    
        public MyTitleBar(Context context, AttributeSet attrs) {
            this(context,attrs,0);
        }
    
        public MyTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyTitleBar, defStyleAttr, 0);
            int count = typedArray.getIndexCount();
            for (int i=0;i<count;i++){
                int attr = typedArray.getIndex(i);
                switch (attr){
                    case R.styleable.MyTitleBar_bar_title:
                        mMyBarTitle = typedArray.getString(attr);
                        break;
                    case R.styleable.MyTitleBar_bar_title_size:
                        mMyBarSize = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                        break;
                }
            }
            typedArray.recycle();
           initView(context);
        }
     private void initView(final Context context) {
            LayoutInflater.from(context).inflate(R.layout.mytitlebar,this);
            TextView bar_title = findViewById(R.id.bar_title);
            Button bt_back = findViewById(R.id.bt_back);
            bt_back.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //调用接口的方法
                    titleBarClickListener.titleBarClicker(v);
                }
            });
            bar_title.setText(mMyBarTitle);
            bar_title.setTextSize(mMyBarSize);
        }
        //定义接口的成员
        onTitleBarClickListener titleBarClickListener;
        //接口的setter,提供给外部的方法
        public void setonTitleBarClickListener(onTitleBarClickListener titleBarClickListener){
            //在setter中把这个接口的实现赋值给这个loginview的上面定义的接口
            this.titleBarClickListener=titleBarClickListener;
        }
        //接口  连接内部与外部
        public interface  onTitleBarClickListener{
            public void  titleBarClicker(View v);
        }
    }
    
    
    3、自定义attrs
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
       <attr name="bar_title" format="string"/>
        <attr name="bar_title_size" format="dimension"/>
        <declare-styleable name="MyTitleBar">
            <attr name="bar_title"/>
            <attr name="bar_title_size"/>
        </declare-styleable>
    </resources>
    
    4、使用
    <?xml version="1.0" encoding="utf-8"?>
    <com.example.intercept.MyRelative
        android:id="@+id/rl_root"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.intercept.MainActivity">
    <com.example.intercept.MyView.MyTitleBar
        android:id="@+id/mytitlebar"
        app:bar_title="标题栏"
        app:bar_title_size="16sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></com.example.intercept.MyView.MyTitleBar>
        <com.example.intercept.MyImageView
            android:id="@+id/iv"
            android:layout_centerInParent="true"
            android:layout_width="100dp"
            android:src="@mipmap/ic_launcher"
            android:layout_height="100dp" />
    </com.example.intercept.MyRelative>
    
    5、点击事件的获取
       MyTitleBar mytitlebar = findViewById(R.id.mytitlebar);
            mytitlebar.setonTitleBarClickListener(new MyTitleBar.onTitleBarClickListener() {
                @Override
                public void titleBarClicker(View v) {
                    if (v.getId()==R.id.bt_back){
                        finish();
                    }
                }
            });
    

    相关文章

      网友评论

        本文标题:自定义组合控件的属性复用

        本文链接:https://www.haomeiwen.com/subject/riazmftx.html