Android自定义View
【摘要】 本文章主要介绍Android系统中,自定义View的开发。通常用于展示特定的样式,或抽象控件,方便复用。1.自定义控件的传参定义declare-styleable,方便从XML布局文件中传入类内部。文件目录:res\values\atts.xml文件内容:<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleab...
本文章主要介绍Android系统中,自定义View的开发。
通常用于展示特定的样式,或抽象控件,方便复用。
1.自定义控件的传参
定义declare-styleable,方便从XML布局文件中传入类内部。
文件目录:
res\values\atts.xml
文件内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SupEditText">
<attr name="isFilters" format="boolean" />
<attr name="emptyTextSize" format="dimension" />
<attr name="textSize" format="dimension" />
<attr name="errorHintColor" format="color" />
<attr name="errorTextSize" format="dimension" />
<attr name="errorText1" format="dimension|string" />
<attr name="errorText2" format="dimension|string" />
</declare-styleable>
</resources>
2.自定义类
以EditText为例,展示获取XML中设置的变量
package com.lizheblogs.view;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatEditText;
import com.teenysoft.jdxs.R;
public class SupEditText extends AppCompatEditText {
private boolean isFilters = true;
private float emptyTextSize = 12;
private float textSize = 14;
private int errorHintColor;
private float errorTextSize = 14;
private String errorText1;
private String errorText2;
public SupEditText(Context context) {
super(context);
init(context, null);
}
public SupEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public SupEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
Resources resources = context.getResources();
errorHintColor = resources.getColor(R.color.edit_text_error_text);
if (attrs != null) {
//从xml的属性中获取到字体颜色与string
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SupEditText);
isFilters = ta.getBoolean(R.styleable.SupEditText_isFilters, true);
emptyTextSize = ta.getDimension(R.styleable.SupEditText_emptyTextSize, 12);
textSize = ta.getDimension(R.styleable.SupEditText_textSize, 14);
errorHintColor = ta.getColor(R.styleable.SupEditText_errorHintColor,
errorHintColor);
errorTextSize = ta.getDimension(R.styleable.SupEditText_errorTextSize, 14);
errorText1 = ta.getString(R.styleable.SupEditText_errorText1);
errorText2 = ta.getString(R.styleable.SupEditText_errorText2);
ta.recycle();
}
}
}
这样就可以通过XML中的设置,来改变控件的样式。
3.XML布局
在XML布局中设置变量的值。
以EditText为例,展示XML中设置变量
<?xml version="1.0" encoding="utf-8"?>
<com.lizheblogs.view.SupEditText
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ts="http://schemas.android.com/apk/res-auto"
android:id="@+id/userNameET"
android:layout_width="match_parent"
android:layout_height="match_parent"
ts:emptyTextSize="12sp"
ts:errorHintColor="@color/edit_text_error_text"
ts:errorText1="@string/input_user_name_please"
ts:errorTextSize="14sp"
ts:isFilters="true"
ts:textSize="14sp" />
ts为空间名,可随意。
大功告成!
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)