自定义控件的使用步骤

最近在跟着老师学习开发简易版微博APP,于是想着把所学的重要知识点记录下来。

自定义控件的开发步骤,主要分为4步:

1.编写attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="LineItem">
        <attr name="rightimage" format="reference"/>
        <attr name="midcontent" format="string"/>
        <attr name="lineShown" format="boolean" />
    </declare-styleable>
</resources>

2.编写布局文件,对自定义属性进行封装

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical"
    >
    <ImageView
        android:id="@+id/iv_seperator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/iv_seperator"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_customcontent"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_weight="10"
            android:textSize="@dimen/text_size_14"
            />
        <ImageView
            android:id="@+id/ic_right"
            android:layout_marginRight="5dp"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:src="@drawable/ic_right"
            />
    </LinearLayout>

3.编写自定义控件,在代码获取自定义控件的属性

public class LineItem extends RelativeLayout {
    private Context mContext;
    private LayoutInflater mInflater;
    private RelativeLayout layout;
    private ImageView mRightIV;
    private ImageView mLineImageView;
    private TextView mMidContent;
    private Boolean lineShown;

    public LineItem(Context context) {
        this(context, null);
    }

    public LineItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LineItem(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.layout = (RelativeLayout) this.mInflater.inflate(R.layout.item_line, this, true);
        mRightIV = (ImageView) this.layout.findViewById(R.id.ic_right);
        mLineImageView = (ImageView) this.layout.findViewById(R.id.iv_seperator);
        mMidContent = (TextView) this.layout.findViewById(R.id.tv_customcontent);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LineItem);
        this.mMidContent.setText(array.getString(R.styleable.LineItem_midcontent));
        this.lineShown = array.getBoolean(R.styleable.LineItem_lineShown, true);
        this.mRightIV.setImageResource(array.getResourceId(R.styleable.LineItem_rightimage, R.drawable.ic_right));
        if (!lineShown) {
            mLineImageView.setVisibility(View.GONE);
        }
        array.recycle();
    }

}

4.使用自定义属性,在XML中赋值(也可以通过代码赋值)

<com.sina.weibo.sdk.demo.LineItem
                android:id="@+id/item_my_topic"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                lin:rightimage="@drawable/ic_right"
                lin:midcontent="我的微博"
                lin:lineshow="false"
                />
            <com.sina.weibo.sdk.demo.LineItem
                android:id="@+id/item_my_home"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                lin:rightimage="@drawable/ic_right"
                lin:midcontent="我的主页"
                lin:lineshow="false"
                />

            <com.sina.weibo.sdk.demo.LineItem
                android:id="@+id/item_my_collect"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                lin:rightimage="@drawable/ic_right"
                lin:midcontent="我的收藏"
                lin:lineshow="true"
                />

 

posted @ 2017-04-24 14:56  汕大小吴  阅读(210)  评论(0编辑  收藏  举报