1. 程式人生 > >android開發:自定義組合控制元件

android開發:自定義組合控制元件

內容介紹

  • 本文記錄,自定義組合控制元件,為了可以程式碼複用,減少程式碼量

配置控制元件屬性檔案
開啟res/values/目錄下的arss.xml檔案,新增下面屬性程式碼,如果沒有建立arrs.xml檔案。

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="TextItemVew">
        <attr name="left_text" format="string"/>
        <attr
name="right_text" format="string"/>
</declare-styleable> </resources>

佈局檔案

<?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">

    <LinearLayout
android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ll_content" android:padding="15dp" android:gravity="center_vertical">
<TextView android:id="@+id/tv_left_text" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_alignParentLeft="true" android:text="user_name" android:textColor="#000000"/>
<TextView android:id="@+id/tv_right_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="liping" android:textColor="#000000" android:gravity="center_vertical|right" android:layout_alignParentRight="true" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_below="@+id/ll_content" android:background="#FFFFFF"/> </RelativeLayout>

自定義控制元件的java檔案


/**
 * 自定義顯示文字控制元件
 * creator: ZZF
 * careate date: 2018/5/17  14:19.
 */

public class TextItemVew extends RelativeLayout {

    private String left_text;
    private String right_text;
    private TextView tv_left_text;
    private TextView tv_right_text;

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

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



    public TextItemVew(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public TextItemVew(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }
    private void initTypedArray(Context context, AttributeSet attrs) {
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.TextItemVew);
        left_text = mTypedArray.getString(R.styleable.TextItemVew_left_text);
        right_text = mTypedArray.getString(R.styleable.TextItemVew_right_text);
        //獲取資源後要及時回收
        mTypedArray.recycle();
    }
    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_text_item, this, true);
        tv_left_text = (TextView)findViewById(R.id.tv_left_text);
        tv_right_text = (TextView)findViewById(R.id.tv_right_text);

        tv_left_text.setText(left_text);
        tv_right_text.setText(right_text);
    }

    public TextView getTv_left_text() {
        return tv_left_text;
    }

    public void setTv_left_text(TextView tv_left_text) {
        this.tv_left_text = tv_left_text;
    }

    public TextView getTv_right_text() {
        return tv_right_text;
    }

    public void setTv_right_text(TextView tv_right_text) {
        this.tv_right_text = tv_right_text;
    }
}

自定義組合控制元件的使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.happycomehealthy.module.test.TestActivity">

    <com.happycomehealthy.widget.TextItemVew
        android:id="@+id/tiv_user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:left_text="姓名">

    </com.happycomehealthy.widget.TextItemVew>


</LinearLayout>

public class TestActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        TextItemVew tiv_user_name = (TextItemVew) findViewById(R.id.tiv_user_name);
        tiv_user_name.getTv_right_text().setText("liping");
    }

}