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");
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值