最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

圖文詳解自定義View視圖的屬性及引用

 更新時(shí)間:2023年04月04日 08:49:50   作者:小白的成長(zhǎng)之路  
這篇文章主要介紹了圖文詳解自定義View視圖的屬性及引用,由于Android自帶的視圖無(wú)法滿足自己需求,又或者美觀度不夠自己的要求,我們就要自來(lái)親自設(shè)計(jì)自己的視圖,需要的朋友可以參考下

本章講解:自定義視圖,我們需要做哪些準(zhǔn)備!

對(duì)于一些中級(jí)的開發(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:打開我們創(chuàng)建好的資源文件,來(lái)寫我們需要的屬性,我簡(jiǎn)單的寫了兩個(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)示例

    Android 畫中畫模式的實(shí)現(xiàn)示例

    這篇文章主要介紹了Android 畫中畫模式的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Android官方下拉刷新控件SwipeRefreshLayout使用詳解

    Android官方下拉刷新控件SwipeRefreshLayout使用詳解

    這篇文章主要為大家詳細(xì)介紹了Android官方下拉刷新控件SwipeRefreshLayout使用方法,實(shí)例展示如何使用下拉刷新控件,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android補(bǔ)間動(dòng)畫的實(shí)現(xiàn)示例

    Android補(bǔ)間動(dòng)畫的實(shí)現(xiàn)示例

    本文主要介紹了Android補(bǔ)間動(dòng)畫的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 深入探討Unit Testing in Android

    深入探討Unit Testing in Android

    本篇文章是對(duì)Unit Testing in Android進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 詳解Android studio 3+版本apk安裝失敗問(wèn)題

    詳解Android studio 3+版本apk安裝失敗問(wèn)題

    這篇文章主要介紹了詳解Android studio 3+版本apk安裝失敗問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • android 手機(jī)截取長(zhǎng)屏實(shí)例代碼

    android 手機(jī)截取長(zhǎng)屏實(shí)例代碼

    本篇文章主要介紹了android 手機(jī)截取長(zhǎng)屏實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • 開源電商app常用標(biāo)簽

    開源電商app常用標(biāo)簽"hot"之第三方開源LabelView

    這篇文章主要介紹了開源電商app常用標(biāo)簽"hot"之第三方開源LabelView,對(duì)開源電商app相關(guān)資料感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • Android 一鍵清理、內(nèi)存清理功能實(shí)現(xiàn)

    Android 一鍵清理、內(nèi)存清理功能實(shí)現(xiàn)

    這篇文章主要介紹了Android 一鍵清理、內(nèi)存清理功能實(shí)現(xiàn),非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
    2017-01-01
  • kotlin android extensions 插件實(shí)現(xiàn)示例詳解

    kotlin android extensions 插件實(shí)現(xiàn)示例詳解

    這篇文章主要為大家介紹了kotlin android extensions 插件實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Kotlin線程同步的幾種實(shí)現(xiàn)方法

    Kotlin線程同步的幾種實(shí)現(xiàn)方法

    面試的時(shí)候經(jīng)常會(huì)被問(wèn)及多線程同步的問(wèn)題,在 Kotlin 中我們有多種實(shí)現(xiàn)方式,本文將所有這些方式做了整理,感興趣的小伙伴們可以參考一下
    2021-07-07

最新評(píng)論

二连浩特市| 绥芬河市| 民权县| 海安县| 南平市| 墨江| 肥西县| 康保县| 墨江| 成安县| 安西县| 永安市| 观塘区| 奉节县| 金乡县| 平阴县| 岳阳县| 临泽县| 潮安县| 慈溪市| 岳阳市| 德令哈市| 蒙城县| 类乌齐县| 泗阳县| 子洲县| 禄劝| 噶尔县| 元朗区| 合江县| 南溪县| 浪卡子县| 班戈县| 孟连| 武鸣县| 宁海县| 长宁县| 庐江县| 红桥区| 盈江县| 宁南县|