美文网首页
简单的自定义组合控件

简单的自定义组合控件

作者: 我是你森哥哥 | 来源:发表于2017-06-08 15:37 被阅读0次

    效果展示图

    捕获.PNG

    1.继承 RelativeLayout 串联构造方法

    public class CustomPhotoOpenView extends RelativeLayout {
        private ImageView iv1,iv2;
        private TextView tv;
    
        public CustomPhotoOpenView(Context context) {
            this(context,null);
        }
    
        public CustomPhotoOpenView(Context context, AttributeSet attrs) {
            this(context, attrs,-1);
        }
    
        public CustomPhotoOpenView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            initView();
            initAttrs(context,attrs);
        }
    

    2.初始化布局文件

    private void initView() {
            View view = View.inflate(getContext(), R.layout.custom_item_photoopen, null);
            this.addView(view);
    
            iv1 = (ImageView) view.findViewById(R.id.iv_custom_photoopen_open);
            iv2 = (ImageView) view.findViewById(R.id.iv_custom_photoopen_photo);
    
            tv = (TextView) view.findViewById(R.id.tv_custom_photoopen_text);
        }
    

    3. 强属性赋给控件

    //把自定义属性设置给自定义控件,并没有什么意义,因为文本在显示的时候是在自定义控件的布局文件中的textview中显示的
    //所以,为了能显示文本,需要在自定义控件中将自定义属性的值获取出来,设置给textview进行显示
    //控件的所有的属性都保存在attrs中,所以可以在attrs中获取属性的值
    //通过命名空间和属性的名称,获取属性的值

     private void initAttrs(Context context, AttributeSet attrs) {
    
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomPhotoOpenView);
    
            String title= ta.getString(R.styleable.CustomPhotoOpenView_pttext);
            int open = ta.getResourceId(R.styleable.CustomPhotoOpenView_ptopen, -1);
            int photo = ta.getResourceId(R.styleable.CustomPhotoOpenView_ptphoto, -1);
    
            tv.setText(title);
            iv1.setImageResource(open);
            iv2.setImageResource(photo);
    
            ta.recycle();
    
        }
    

    布局文件如下

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mine_integral"
        android:layout_width="match_parent"
        android:layout_height="@dimen/y25">
    
        <ImageView
            android:id="@+id/iv_custom_photoopen_photo"
            android:layout_width="@dimen/x20"
            android:layout_height="@dimen/y20"
            android:layout_centerVertical="true"
            android:layout_marginLeft="@dimen/x15"
            android:src="@mipmap/wodejfen" />
    
        <TextView
            style="@style/Mine_More"
            android:id="@+id/tv_custom_photoopen_text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_toRightOf="@id/iv_custom_photoopen_photo"
            android:text="我的积分" />
    
        <ImageView
            android:id="@+id/iv_custom_photoopen_open"
            style="@style/style_qianwang" />
    </RelativeLayout>
    

    相关文章

      网友评论

          本文标题:简单的自定义组合控件

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