圖文詳解自定義View視圖的屬性及引用
本章講解:自定義視圖,我們需要做哪些準(zhǔn)備!
對(duì)于一些中級(jí)的開(kāi)發(fā)者來(lái)說(shuō)就要接觸到自定義視圖,由于Android自帶的視圖無(wú)法滿足自己需求,又或者美觀度不夠自己的要求,我們就要自來(lái)親自設(shè)計(jì)自己的視圖。那么如何來(lái)實(shí)現(xiàn)自定義視圖呢?下面我們先簡(jiǎn)單的來(lái)認(rèn)識(shí)下如何實(shí)現(xiàn)自定義視圖!
第一步 自定義視圖首先需要什么?我們都要做那些簡(jiǎn)單的準(zhǔn)備?
1、我們需要?jiǎng)?chuàng)建一個(gè)類,來(lái)繼承View
2、我們需要自己去實(shí)現(xiàn)自定義視圖需求的各種資源屬性
3、引用我們定義好的自定義屬性
4、我們還經(jīng)常用到3個(gè)方法onMeasure(),onLayout(),onDraw(),(這里先不講)
一、創(chuàng)建一個(gè)類,繼承View
會(huì)提示我們添加構(gòu)造方法,它擁有4個(gè),我們起碼要用2個(gè),如下

二、如何創(chuàng)建自定義屬性呢?
2-1:創(chuàng)建一個(gè)資源文件

創(chuàng)建成功

2-2:打開(kāi)我們創(chuàng)建好的資源文件,來(lái)寫(xiě)我們需要的屬性,我簡(jiǎn)單的寫(xiě)了兩個(gè),如圖:

注意:自定義屬性的過(guò)程及屬性和對(duì)應(yīng)的類別
>自定義屬性: 1. reference:參考某一資源ID,以此類推 (1)屬性定義: <declare-styleable name = "名稱"> <attr name = "background" format = "reference" /> </declare-styleable> (2)屬性使用: <ImageView android:layout_width = "42dip" android:layout_height = "42dip" android:background = "@drawable/圖片ID" /> 2. color:顏色值 <declare-styleable name = "名稱"> <attr name = "textColor" format = "color" /> </declare-styleable> 3. boolean:布爾值 <declare-styleable name = "名稱"> <attr name = "focusable" format = "boolean" /> </declare-styleable> 4. dimension:尺寸值。注意,這里如果是dp那就會(huì)做像素轉(zhuǎn)換 <declare-styleable name = "名稱"> <attr name = "layout_width" format = "dimension" /> </declare-styleable> 5. float:浮點(diǎn)值。 6. integer:整型值。 7. string:字符串 8. fraction:百分?jǐn)?shù)。 9. enum:枚舉值 10. flag:是自己定義的,類似于 android:gravity="top",就是里面對(duì)應(yīng)了自己的屬性值。 11. reference|color:顏色的資源文件。 12.reference|boolean:布爾值的資源文件
三、如何引用我們的自定義的資源
可以通過(guò)TypedArray 類來(lái)接受我們自定義的屬性,也可以在xml中來(lái)指定我們自定義的屬性
3-1:在代碼中引用
public class ViewDemo extends View {
public ViewDemo(Context context) {
super(context);
}
public ViewDemo(Context context, AttributeSet attrs) {
super(context, attrs);
/**
* TypedArray:對(duì)象描述類似數(shù)組的一個(gè)潛在的二進(jìn)制數(shù)據(jù)的緩沖區(qū)(官方描述)
* 就是系統(tǒng)在默認(rèn)的資源文件R.styleable中去獲取相關(guān)的配置。
* 如果appearance不為空,它就會(huì)去尋找獲取相關(guān)屬性
* 也就是沖我們自定屬性樣式中,來(lái)引用你需要的某條屬性
*/
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.Myview);
int colors =typedArray.getColor(R.styleable.Myview_rect_color,0xffff0000);//給他賦值一個(gè)紅色
setBackgroundColor(colors);
typedArray.recycle();
}
}
引用后設(shè)置為顏色為紅色,我們布局中的組件就會(huì)變成紅色

3-2:引用我們的資源?
我們還可以在我們的布局中直接引用我們的屬性,如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:attrs_ViewDemo="http://schemas.android.com/apk/res/tester.ermu.com.viewtext"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="ResAuto">
<tester.ermu.com.viewtext.ViewDemo
android:layout_width="match_parent"
android:layout_height="50dp"
attrs_ViewDemo:rect_color = "#ff00ff00"/>
</LinearLayout>
運(yùn)行效果,已經(jīng)覆蓋紅色

附上所有的代碼 MainActivity
package tester.ermu.com.viewtext;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
ViewDemo
package tester.ermu.com.viewtext;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by ENZ on 2016/11/17.
* 1、我們讓ViewDemo繼承View
* 2、他有4個(gè)構(gòu)造方法,我們常用的有兩個(gè),這里我全部添加進(jìn)來(lái)
*
*/
public class ViewDemo extends View {
public ViewDemo(Context context) {
super(context);
}
public ViewDemo(Context context, AttributeSet attrs) {
super(context, attrs);
/**
* TypedArray:對(duì)象描述類似數(shù)組的一個(gè)潛在的二進(jìn)制數(shù)據(jù)的緩沖區(qū)(官方描述)
* 就是系統(tǒng)在默認(rèn)的資源文件R.styleable中去獲取相關(guān)的配置。
* 如果appearance不為空,它就會(huì)去尋找獲取相關(guān)屬性
* 也就是沖我們自定屬性樣式中,來(lái)引用你需要的某條屬性
*/
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.Myview);
int colors =typedArray.getColor(R.styleable.Myview_rect_color,0xffff0000);//給他賦值一個(gè)紅色
setBackgroundColor(colors);
typedArray.recycle();
}
}
main布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="tester.ermu.com.viewtext.MainActivity">
<include
layout="@layout/activity_viewdemo" />
</RelativeLayout>
自定義view布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:attrs_ViewDemo="http://schemas.android.com/apk/res/tester.ermu.com.viewtext"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="ResAuto">
<tester.ermu.com.viewtext.ViewDemo
android:layout_width="match_parent"
android:layout_height="50dp"
attrs_ViewDemo:rect_color = "#ff00ff00"/>
</LinearLayout>
自定義屬性資源
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--我們自定義的屬性,名字為Myview,attr是其中屬性的意思,format時(shí)指定這條屬性是屬于什么類型-->
<declare-styleable name="Myview">
<attr name="rect_color" format="color"></attr>
<attr name="rect_text" format="reference"></attr>
</declare-styleable>
</resources>
到此這篇關(guān)于圖文詳解自定義View視圖的屬性及引用的文章就介紹到這了,更多相關(guān)自定義View視圖屬性引用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android實(shí)現(xiàn)多點(diǎn)觸摸效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)多點(diǎn)觸摸效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android Studio導(dǎo)入Project與Module的方法及實(shí)例
這篇文章主要介紹了Android Studio導(dǎo)入Project與Module的方法及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android中TelephonyManager類的用法案例詳解
這篇文章主要介紹了Android中TelephonyManager類的用法,以獲取Android手機(jī)硬件信息為例詳細(xì)分析了TelephonyManager類的使用技巧,需要的朋友可以參考下2015-09-09
創(chuàng)建子線程對(duì)Android進(jìn)行網(wǎng)絡(luò)訪問(wèn)
這篇文章介紹了Android中創(chuàng)建子線程進(jìn)行網(wǎng)絡(luò)訪問(wèn)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考2021-11-11
Android中Glide獲取圖片Path、Bitmap用法詳解
這篇文章主要介紹了Android中Glide獲取圖片Path、Bitmap用法以及代碼分析,需要的朋友們參考一下吧。2017-12-12
android MediaRecorder實(shí)現(xiàn)錄屏?xí)r帶錄音功能
這篇文章主要介紹了android MediaRecorder錄屏?xí)r帶錄音功能實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Android自定義View實(shí)現(xiàn)環(huán)形進(jìn)度條的思路與實(shí)例
最近看到豆瓣FM的音樂(lè)播放界面,有一個(gè)環(huán)形的進(jìn)度條挺不錯(cuò)的,最近有空就想著實(shí)現(xiàn)了,所以下面這篇文章主要給大家介紹了Android自定義View實(shí)現(xiàn)環(huán)形進(jìn)度條的思路與實(shí)例,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-04-04
Android仿抖音主頁(yè)效果實(shí)現(xiàn)代碼
這篇文章主要介紹了Android仿抖音主頁(yè)效果實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Android中Fragmen首選項(xiàng)使用自定義的ListPreference的方法
Android中Fragmen的首選項(xiàng)可以使用自定義的ListPreference,這樣Fragment的PreferenceFragment就可以更方便地保存配置信息,需要的朋友可以參考下2016-05-05

