为什么使用自定义控件?
一般像我们初学者,在做界面的过程中输入框(EditView)是经常用到的。为了用户填写信息区别开来我们往往会在EditView前面加个文本框(TextView)类似的输入(姓名:、年龄:…)。那么问题来了如果要输入的信息有7到8条呢,是不是很麻烦,这其中还会涉及到布局问题。所以我分享一种更方便的方法让他们两个控件变成一个怎么样。
my_edit_view.xml
// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:textSize="20sp"
android:gravity="center"
android:text="name"
android:id="@+id/tv_name"
android:layout_width="80dp"
android:layout_height="40dp" />
<EditText
android:layout_marginLeft="80dp"
android:hint="please enter"
android:id="@+id/et_conter"
android:layout_width="match_parent"
android:layout_height="40dp" />
</merge>
这是一个简单的布局
效果图如下:
接下来再看看我们的自定义控件类
MyEditText .class
package com.example.syt98.myappcustomview;
/**
* Created by syt98 on 2018/12/10.
*/
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MyEditText extends RelativeLayout {
private TextView tv_name;
private EditText et_conter;
private String tname;
private int etback;
public MyEditText(Context context) {
super(context);
init();
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyEditText);
// 解析集合中的属性tvName属性
// 该属性的id为:R.styleable.MyEditText_tvName
// 将解析的属性传入到变量当中
tname = a.getText(R.styleable.MyEditText_tvName).toString().trim();
etback = a.getColor(R.styleable.MyEditText_etBack,Color.WHITE);
// 解析后释放资源
a.recycle();
init();
}
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
inflate(getContext(), R.layout.my_edit_view, this);
this.tv_name = (TextView)findViewById(R.id.tv_name);
this.et_conter = (EditText) findViewById(R.id.et_conter);
tv_name.setText(tname);
et_conter.setBackgroundColor(etback);
}
}
在写这个类之前呢,我们应该把attr.xml中的自定义属性和main_activity.xim中控件写好。
attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyEditText">
<attr name="tvName" format="string"/>
<attr name="etBack" format="reference|color"/>
</declare-styleable>
</resources>
attr.xml需放在values下面,
其中的format代表的是自定义属性的值类型,像我想设置输入框背景(etBack),那么它的属性值就有颜色或者导入用"reference|color"表示。
main_activity.xml
<?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.example.syt98.myappcustomview.MainActivity">
<com.example.syt98.myappcustomview.MyEditText
app:etBack="#bbf4ff"
app:tvName="套你猴子"
android:layout_width="match_parent"
android:layout_height="40dp">
</com.example.syt98.myappcustomview.MyEditText>
</LinearLayout>
在自定义控件中的自定义属性都是app哦。
运行效果图: