汕頭市手機(jī)網(wǎng)站建設(shè)品牌steam交易鏈接在哪里
一、概念?
在 xml 中為控件設(shè)置的屬性。自定義屬性名稱如果使用系統(tǒng)已定義的,例如 textSize 會在編譯時(shí)報(bào)錯(cuò)。
格式類型 | 定義/使用 |
string 字符串 | <attr name = "myContent" format = "color" /> android:myContent = "Hello Word!" |
color 顏色 | <attr name = "myTextColor" format = "color" /> android:myTextColor = "#00FF00" |
dimension 尺寸 | <attr name = "myTextSize" format = "dimension" /> android:myTextSize = "12.sp" |
reference 資源 | <attr name = "myBackground" format = "reference" /> android:myBackground = "@drawable/圖片ID" |
boolean 布爾 | <attr name = "myEnable" format = "boolean" /> android:myEnable = "true" |
float 浮點(diǎn) | <attr name = "myAlpha" format = "float" /> android:myAlpha = "0.5F" |
integer 整型 | <attr name = "myMaxLines" format = "integer" /> android:myMaxLines = "3" |
fraction 百分比 | <attr name = "myOffset" format = "fraction" /> android:myOffset = "10%" |
enum 枚舉 | <attr name = "myOrientation"> ? ? ? ? <enum name = "horizontal" value="0" /> ????????<enum name = "vertical" value="1" /> </attr> android:myOrientation = "vertical" |
flag 位運(yùn)算 | 位運(yùn)算類型的屬性在使用的過程中可以使用多個(gè)值 <attr name = "myGravity" /> ? ? ? ? <flag nema="top" value="0x01"> ????????<flag nema="left" value="0x02"> ????????<flag nema="center_vertical" value="0x02"> </attr> android:myGravity = "top|left" |
混合類型 | 屬性定義時(shí)可以指定多種類型值 <attr name = "myBackground" format = "reference|color" /> android:myBackground = "@drawable/圖片ID" android:myBackground = "#00FF00" |
二、自定義步驟
2.1 創(chuàng)建資源文件(屬性聲明)
右鍵 values 目錄 -> New File文件 -> 一般取名attrs.xml。
<resources><!--name使用自定義View的名稱--><declare-styleable name="MyView"><!--name屬性名稱,format格式--><attr name="myText" format="string" /><attr name="myTextColor" format="color" /><attr name="myTextSize" format="dimension" /><attr name="myMaxLength" format="integer" /><attr name="myBackground" format="reference|color" /><!--枚舉--><attr name="myInputType"><enum name="number" value="1"/><enum name="text" value="2"/></attr></declare-styleable>
</resources>
2.2?構(gòu)造函數(shù)中配置
constructor(context: Context?) : super(context) 重寫一個(gè)參數(shù)的構(gòu)造函數(shù),使用場景:代碼?new 創(chuàng)建實(shí)例的時(shí)候調(diào)用。 |
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) 重寫兩個(gè)參數(shù)的構(gòu)造函數(shù),使用場景:xml中使用時(shí)調(diào)用(xml轉(zhuǎn)java代碼的時(shí)候反射)。 |
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) 重寫三個(gè)參數(shù)的構(gòu)造函數(shù),使用場景:使用主題Style的時(shí)候調(diào)用。 |
class MyView : View {private var text: String? = nullprivate var textSize: Int? = nullprivate var textColor: Int? = nullprivate var maxLength: Int? = nullprivate var background: Int? = nullprivate var inputType: Int? = null//改成this調(diào)用2個(gè)參數(shù)的構(gòu)造constructor(context: Context?) : this(context, null)//改成this調(diào)用3個(gè)參數(shù)的構(gòu)造constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)//在這里統(tǒng)一進(jìn)行處理constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {context?.let {//返回一個(gè)與attrs中列舉出的屬性相關(guān)的數(shù)組,數(shù)組里面的值由樣式屬性指定val attributes = it.obtainStyledAttributes(attrs, R.styleable.MyView)//獲取自定義屬性(格式:屬性名稱_條目名稱)text = attributes.getString(R.styleable.MyView_myText)textSize = attributes.getDimensionPixelSize(R.styleable.MyView_myTextSize, 0)textColor = attributes.getColor(R.styleable.MyView_myTextColor, Color.BLACK)maxLength = attributes.getInt(R.styleable.MyView_myMaxLength,1)background = attributes.getResourceId(R.styleable.MyView_myBackground,R.drawable.ic_launcher_foreground)inputType = attributes.getInt(R.styleable.MyView_myInputType,0)//回收資源attributes.recycle()}}
}
2.3?布局中使用(屬性使用)
- 根布局添加命名空間(只需要輸入app,IDE會自動(dòng)補(bǔ)全)。
- 控件名稱使用完整路徑(只需要輸入自定義View的類名,IDE會自動(dòng)補(bǔ)全)。
- 未自定義的屬性View會去處理(繼承自View),使用自定義的屬性就是 app: 開頭的。
<!--根布局添加命名空間-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!--控件名稱使用完整路徑--><com.example.kotlindemo.view.MyViewandroid:layout_width="match_parent"android:layout_height="wrap_content"app:myBackground="@drawable/ic_launcher_foreground"app:myInputType="number"app:myText="Hello Word!"app:myTextColor="@color/black"app:myTextSize="20sp"app:myMaxLength="20"/>
</LinearLayout>