Android 将布局变成 控件

在项目开发的时候,有时会出现多个相同的布局,如果复制黏贴,会增加代码的重复量,所以可以将布局控件化,通过属性的变化来控制布局的相应变化。

如:先画出一个通用布局的layout,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/colorPrimary">

    <TextView
        android:id="@+id/first_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="张三"
        tools:textColor="@color/white"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"/>

    <TextView
        android:id="@+id/second_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="李四"
        tools:textColor="@color/white"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"/>

    <TextView
        android:id="@+id/thread_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="王五"
        tools:textColor="@color/white"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"/>

</LinearLayout>

这个只是一个简单的三个 TextView

通过属性改 文字内容与 颜色

需要在根布局 加   

xmlns:tools="http://schemas.android.com/tools"

将需要的改的属性,可以使用

tools:text="张三"
tools:textColor="@color/white"

接下来,需要设置属性: 在 res/values 下创建 attrs.xml 文件,将需要的属性进行定义,此次 有三个TextView 分别有文本跟颜色,所以需要 6个属性

<declare-styleable name="MtTextTest">
    <attr name="first_note_text" format="string"/>
    <attr name="first_text_color" format="string"/>
    <attr name="second_note_text" format="string"/>
    <attr name="second_text_color" format="string"/>
    <attr name="thread_note_text" format="string"/>
    <attr name="thread_text_color" format="string"/>
</declare-styleable>

下一步,自定义View,使其可以在其他布局引用

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.duke.mvvmtesr.R;


public class MtTextTest extends FrameLayout {
    private View mView;
    private Context mContext;
    private TextView first_tv;
    private TextView second_tv;
    private TextView thread_tv;

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

    public MtTextTest(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MtTextTest(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    private void setText(String flag, String note) {
        if (note != null && !note.equals("")) {
            if (flag.equals("1")) {
                first_tv.setText(note);
            } else if (flag.equals("2")) {
                second_tv.setText(note);
            } else {
                thread_tv.setText(note);
            }
        } else {
            if (flag.equals("1")) {
                first_tv.setText("给个空的干嘛!");
            } else if (flag.equals("2")) {
                second_tv.setText("给个空的干嘛!");
            } else {
                thread_tv.setText("给个空的干嘛!");
            }
        }

    }

    private void setTextColor(String flag, String color) {
        if (flag.equals("1")) {
            first_tv.setTextColor(Color.parseColor(color));
        } else if (flag.equals("2")) {
            second_tv.setTextColor(Color.parseColor(color));
        } else {
            thread_tv.setTextColor(Color.parseColor(color));
        }
    }


    private void init(Context context, AttributeSet attrs) {
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.text_item_layout, this, true);
        first_tv = mView.findViewById(R.id.first_tv);
        second_tv = mView.findViewById(R.id.second_tv);
        thread_tv = mView.findViewById(R.id.thread_tv);

        TypedArray a = mContext.obtainStyledAttributes(attrs,R.styleable.MtTextTest);
        setText("1",a.getString(R.styleable.MtTextTest_first_note_text));
        setText("2",a.getString(R.styleable.MtTextTest_second_note_text));
        setText("3",a.getString(R.styleable.MtTextTest_thread_note_text));
        setTextColor("1",a.getString(R.styleable.MtTextTest_first_text_color));
        setTextColor("2",a.getString(R.styleable.MtTextTest_second_text_color));
        setTextColor("3",a.getString(R.styleable.MtTextTest_thread_text_color));


    }

}

接下来就可以在其他布局中引用了:

  <自定义控件位置.MtTextTest
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:first_note_text="第一个名字"
        app:second_note_text="第二个名字"
        app:thread_note_text="第三个名字"
        app:first_text_color="#7FFF00"
        app:second_text_color="#B22222"
        app:thread_text_color="@color/yellow">

    </自定义控件位置.MtTextTest>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值