Android自定义华为睡眠,Android自定义View
本文章主要介紹Android系統(tǒng)中,自定義View的開發(fā)。
通常用于展示特定的樣式,或抽象控件,方便復(fù)用。
1.自定義控件的傳參
定義declare-styleable,方便從XML布局文件中傳入類內(nèi)部。
文件目錄:
res\values\atts.xml
文件內(nèi)容:
2.自定義類
以EditText為例,展示獲取XML中設(shè)置的變量
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中的設(shè)置,來改變控件的樣式。
3.XML布局
在XML布局中設(shè)置變量的值。
以EditText為例,展示XML中設(shè)置變量
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為空間名,可隨意。
大功告成!
總結(jié)
以上是生活随笔為你收集整理的Android自定义华为睡眠,Android自定义View的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity接入移动MM支付(Androi
- 下一篇: ubuntu两个显示器只出一个_Ubun