詳解Android自定義控件屬性
在Android開發(fā)中,往往要用到自定義的控件來實(shí)現(xiàn)我們的需求或效果。在使用自定義
控件時(shí),難免要用到自定義屬性,那怎么使用自定義屬性呢?
在文件res/values/下新建attrs.xml屬性文件,中定義我們所需要的屬性。
<?xml version="1.0" encoding="utf-8"?>
<resources><!-- resource是跟標(biāo)簽,可以在里面定義若干個(gè)declare-styleable -->
<declare-styleable name="custom_view"><!-- name定義了變量的名稱 -->
<attr name="custom_color" format="color"></attr> <!-- 定義對(duì)應(yīng)的屬性,name定義了屬性的名稱 -->
<attr name="custom_size" format="dimension"></attr> <!--每一個(gè)發(fā)生要定義format指定其類型,類型包括
reference 表示引用,參考某一資源ID
string 表示字符串
color 表示顏色值
dimension 表示尺寸值
boolean 表示布爾值
integer 表示整型值
float 表示浮點(diǎn)值
fraction 表示百分?jǐn)?shù)
enum 表示枚舉值
flag 表示位運(yùn)算
-->
</declare-styleable>
public class CustomTextView extends TextView {
private int textSize;//自定義文件大小
private int textColor;//自定義文字顏色
//自定義屬性,會(huì)調(diào)用帶兩個(gè)對(duì)數(shù)的構(gòu)造方法
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.custom_view);//TypedArray屬性對(duì)象
textSize = ta.getDimensionPixelSize(R.styleable.custom_view_custom_size, 20);//獲取屬性對(duì)象中對(duì)應(yīng)的屬性值
textColor = ta.getColor(R.styleable.custom_view_custom_color, 0x0000ff);
setColorAndSize(textColor, textSize);//設(shè)置屬性
ta.recycle();
}
public CustomTextView(Context context) {
super(context);
}
private void setColorAndSize(int textColor, int textSize) {
setTextColor(textColor);
setTextSize(textSize);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ldm="http://schemas.android.com/apk/res/com.ldm.learn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f6f6f6"
android:orientation="vertical"
android:padding="10dp" >
<com.ldm.learn.CustomTextView
android:layout_width="100dp"
android:layout_height="100dp"
android:text="自定義TextView"
ldm:custom_color="#333333"
ldm:custom_size="35sp" />
</LinearLayout>
布局說明:

通過以上幾步就可以實(shí)現(xiàn)我們想要的自定義屬性效果(用自定義屬性設(shè)置文字大小及顏色)啦!
- Android應(yīng)用開發(fā)中自定義ViewGroup視圖容器的教程
- Android App中自定義View視圖的實(shí)例教程
- android 自定義控件 自定義屬性詳細(xì)介紹
- android開發(fā)教程之自定義屬性用法詳解
- Android中自定義控件的declare-styleable屬性重用方案
- Android自定義屬性 format的深入解析
- 詳解Android自定義控件屬性TypedArray以及attrs
- 理解Android中的自定義屬性
- Android UI設(shè)計(jì)系列之自定義TextView屬性實(shí)現(xiàn)帶下劃線的文本框(4)
- Android如何自定義視圖屬性
相關(guān)文章
unity3d發(fā)布apk在android虛擬機(jī)中運(yùn)行的詳細(xì)步驟(unity3d導(dǎo)出android apk)
這篇文章主要介紹了unity3d發(fā)布apk在android虛擬機(jī)中運(yùn)行的詳細(xì)步驟,需要的朋友可以參考下2014-05-05
Android使用ViewPager實(shí)現(xiàn)圖片滑動(dòng)預(yù)覽效果
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實(shí)現(xiàn)圖片滑動(dòng)預(yù)覽效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android使用Kotlin實(shí)現(xiàn)多節(jié)點(diǎn)進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android使用Kotlin實(shí)現(xiàn)多節(jié)點(diǎn)進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
Andorid基于ZXing實(shí)現(xiàn)二維碼生成與掃描的示例代碼
ZXing是一個(gè)開源的條碼和二維碼掃描庫,它可以用于Android開發(fā)中,通過ZXing庫可以實(shí)現(xiàn)Android設(shè)備上的條碼和二維碼掃描功能,開發(fā)者可以輕松地在Android應(yīng)用中集成條碼和二維碼掃描功能,本文主要給大家介紹了Andorid?ZXing實(shí)現(xiàn)二維碼,感興趣的朋友可以參考下2023-08-08
Kotlin Extension Function擴(kuò)展函數(shù)詳細(xì)介紹
Kotlin支持使用新功能擴(kuò)展類的能力,而無需通過類實(shí)現(xiàn)繼承概念或使用設(shè)計(jì)模式,如裝飾器(Decorator)。這是通過稱為擴(kuò)展功能(Extension Function)的特殊方式來完成的。因此,此功能可以有效地使代碼變得更清晰和易于閱讀,并且還可以減少代碼2023-02-02
Android Studio 新手入門教程(一)基本設(shè)置圖解
這篇文章主要介紹了Android Studio 新手入門教程(一)基本設(shè)置圖解,需要的朋友可以參考下2017-12-12
Android編程獲取并設(shè)置Activity亮度的方法
這篇文章主要介紹了Android編程獲取并設(shè)置Activity亮度的方法,涉及Android針對(duì)屏幕亮度的相關(guān)操作技巧,需要的朋友可以參考下2015-12-12

