網站首頁 編程語言 正文
當我們自定義 View 的時候,至少要定義兩個構造函數。
public CustomView(Context context) {
this(context, null);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
第二個構造函數中的參數 attrs 可以獲取在 xml 中添加的屬性的值。
系統已經給我們定義了以下屬性,可以在 sdk/platforms/android-xx/data/res/values/attrs.xml 中找到。我們也可以自定義屬性。
怎么自定義屬性?
- 在 res/values 下創建 attrs.xml 文件
- 添加如下內容
<resources>
<declare-styleable name="CustomTextView">
<attr name="text" format="string" />
</declare-styleable>
</resources>
declare-styleable 定義屬性分組,名稱一般和自定義 View 的名稱一樣。在 R 文件中就會生成 styleable 類。里面包含所有屬性。
public static final class styleable {
public static final int[] CustomTextView = { 2130903853 };
public static final int CustomTextView_text = 0;
}
attr 定義或者聲明屬性。注意,這邊有兩種方式:
- 定義:后面有 format 的就是定義。
- 聲明:后面沒有 format 的就是聲明。比如:
<attr name="android:text"/>
。當屬性已經在別的地方定義的時候,不能重復定義,否則會報錯。
- 在 xml 中使用屬性
<com.smaple.CustomTextView
android:layout_width="wrap_content"
android:layout_heidht="wrap_content"
app:text = "Hello World!"
/>
- 在 Java 中獲取屬性值
private void init(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
String text = ta.getString(R.styleable.CustomTextView_text);
// 回收
ta.recycle();
}
Attributeset 就是一個屬性集合,內部是一個 XML 解析器,最后解析成 key-value 的形式。
Java 中獲取系統屬性值
如果要在 Java 中獲取系統的屬性值,必須也要在 attrs.xml 中對應的 View 下聲明。
<resources>
<declare-styleable name="CustomTextView">
<attr name="text" format="string" />
<attr name="android:layout_height" />
</declare-styleable>
</resources>
聲明完了就可以在 Java 中獲取了
int height = ta.getDimensionPixelSize(R.styleable.CustomTextView_android_layout_height, 0)
format 類型
format | 解釋 |
---|---|
integer | 整型值 |
float | 浮點值 |
boolean | 布爾值 |
string | 字符串。比如 “hello”、R.string.hello |
enum | 枚舉 |
fraction | 百分數。只能是 xx% |
flag | 位或運算 |
dimension | 尺寸值。比如 10px、10dp、R.dimen.xx |
color | 顏色值。比如 #FFFFFF、R.color.white |
reference | 資源 ID。比如 R.drawable.id |
例子:
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomView">
<attr name="attr_int" format="integer" />
<attr name="attr_float" format="float" />
<attr name="attr_bool" format="boolean" />
<attr name="attr_string" format="string" />
<attr name="attr_enum">
<enum name="type_1" value="0" />
<enum name="type_2" value="1" />
</attr>
<attr name="attr_fraction" format="fraction" />
<attr name="attr_flag">
<flag name="top" value="0x1" />
<flag name="left" value="0x2" />
<flag name="right" value="0x3" />
<flag name="bottom" value="0x4" />
</attr>
<attr name="attr_dimension" format="dimension" />
<attr name="attr_color" format="color" />
<attr name="attr_reference" format="reference" />
</declare-styleable>
</resources>
activity_main.xml
<com.sample.CustomView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:attr_bool="true"
app:attr_color="@color/black"
app:attr_dimension="9dp"
app:attr_enum="type_2"
app:attr_flag="left"
app:attr_reference="@drawable/ic_launcher"
app:attr_float="1.5"
app:attr_fraction="50%"
app:attr_int="1"
app:attr_string="hello" />
CustomView.kt
val ta = context!!.obtainStyledAttributes(attrs, R.styleable.CustomView)
val attrInt = ta.getInt(R.styleable.CustomView_attr_int, 0)
Log.i("tianjf", "attr_int: $attrInt")
val attrFloat = ta.getFloat(R.styleable.CustomView_attr_float, 0f)
Log.i("tianjf", "attr_float: $attrFloat")
val attrBool = ta.getBoolean(R.styleable.CustomView_attr_bool, false)
Log.i("tianjf", "attr_bool: $attrBool")
val attrString = ta.getString(R.styleable.CustomView_attr_string)
Log.i("tianjf", "attr_string: $attrString")
val attrEnum = ta.getInt(R.styleable.CustomView_attr_enum, 0)
Log.i("tianjf", "attr_enum: $attrEnum")
val attrFraction = ta.getFraction(R.styleable.CustomView_attr_fraction, 1, 1, 0f)
Log.i("tianjf", "attr_fraction: $attrFraction")
val attrFlag = ta.getInt(R.styleable.CustomView_attr_flag, 0)
Log.i("tianjf", "attr_flag: $attrFlag")
val attrDimension = ta.getDimension(R.styleable.CustomView_attr_dimension, 0f)
val attrDimensionPixel = ta.getDimensionPixelSize(R.styleable.CustomView_attr_dimension, 0)
Log.i("tianjf", "attr_dimension: $attrDimension")
Log.i("tianjf", "attr_dimension: $attrDimensionPixel px")
val attrColor = ta.getColor(R.styleable.CustomView_attr_color, 0)
Log.i("tianjf", "attr_color: $attrColor")
val attrReference = ta.getDrawable(R.styleable.CustomView_attr_reference)
Log.i("tianjf", "attr_reference: $attrReference")
打印如下:
I/tianjf: attr_int: 1
I/tianjf: attr_float: 1.5
I/tianjf: attr_bool: true
I/tianjf: attr_string: hello
I/tianjf: attr_enum: 1
I/tianjf: attr_fraction: 0.5
I/tianjf: attr_flag: 2
I/tianjf: attr_dimension: 13.5
I/tianjf: attr_dimension: 14 px
I/tianjf: attr_color: -16777216
I/tianjf: attr_reference: android.graphics.drawable.BitmapDrawable@4fe5f57
混合 format 類型
format 還可以添加多個類型,比如系統的 android:background 就是混合類型:
<attr name="background" format="reference|color" />
原文鏈接:https://blog.csdn.net/tianjf0514/article/details/125711558
相關推薦
- 2022-04-03 用Python實現控制電腦鼠標_python
- 2023-03-28 通知監控NotificationListenerService?onNotificationPost
- 2023-03-20 python如何在pygame中設置字體并顯示中文詳解_python
- 2023-02-02 goland中npm無法使用的問題及解決_Golang
- 2022-06-11 FreeRTOS進階之調度器啟動過程分析_操作系統
- 2024-01-14 在springboot中給mybatis加攔截器
- 2023-06-17 Python利用plotly繪制正二十面體詳解_python
- 2022-05-06 Matplotlib安裝與配置
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支