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

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

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

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

對于一些中級的開發(fā)者來說就要接觸到自定義視圖,由于Android自帶的視圖無法滿足自己需求,又或者美觀度不夠自己的要求,我們就要自來親自設計自己的視圖。那么如何來實現(xiàn)自定義視圖呢?下面我們先簡單的來認識下如何實現(xiàn)自定義視圖!

第一步 自定義視圖首先需要什么?我們都要做那些簡單的準備?

1、我們需要創(chuàng)建一個類,來繼承View

2、我們需要自己去實現(xiàn)自定義視圖需求的各種資源屬性

3、引用我們定義好的自定義屬性

4、我們還經常用到3個方法onMeasure(),onLayout(),onDraw(),(這里先不講)

一、創(chuàng)建一個類,繼承View

會提示我們添加構造方法,它擁有4個,我們起碼要用2個,如下

這里寫圖片描述

二、如何創(chuàng)建自定義屬性呢?

2-1:創(chuàng)建一個資源文件

這里寫圖片描述

創(chuàng)建成功

這里寫圖片描述

2-2:打開我們創(chuà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那就會做像素轉換
<declare-styleable name = "名稱">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>

5. float:浮點值。
6. integer:整型值。
7. string:字符串
8. fraction:百分數(shù)。
9. enum:枚舉值
10. flag:是自己定義的,類似于 android:gravity="top",就是里面對應了自己的屬性值。
11. reference|color:顏色的資源文件。
12.reference|boolean:布爾值的資源文件

三、如何引用我們的自定義的資源

可以通過TypedArray 類來接受我們自定義的屬性,也可以在xml中來指定我們自定義的屬性

3-1:在代碼中引用

public class ViewDemo extends View {

    public ViewDemo(Context context) {
        super(context);
    }

    public ViewDemo(Context context, AttributeSet attrs) {
        super(context, attrs);
        /**
         * TypedArray:對象描述類似數(shù)組的一個潛在的二進制數(shù)據(jù)的緩沖區(qū)(官方描述)
         * 就是系統(tǒng)在默認的資源文件R.styleable中去獲取相關的配置。
         * 如果appearance不為空,它就會去尋找獲取相關屬性
         * 也就是沖我們自定屬性樣式中,來引用你需要的某條屬性
        */
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.Myview);
        int colors =typedArray.getColor(R.styleable.Myview_rect_color,0xffff0000);//給他賦值一個紅色
        setBackgroundColor(colors);

        typedArray.recycle();
    }

}

引用后設置為顏色為紅色,我們布局中的組件就會變成紅色

這里寫圖片描述

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>

運行效果,已經覆蓋紅色

這里寫圖片描述

附上所有的代碼 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個構造方法,我們常用的有兩個,這里我全部添加進來
 *
 */

public class ViewDemo extends View {

    public ViewDemo(Context context) {
        super(context);
    }

    public ViewDemo(Context context, AttributeSet attrs) {
        super(context, attrs);
        /**
         * TypedArray:對象描述類似數(shù)組的一個潛在的二進制數(shù)據(jù)的緩沖區(qū)(官方描述)
         * 就是系統(tǒng)在默認的資源文件R.styleable中去獲取相關的配置。
         * 如果appearance不為空,它就會去尋找獲取相關屬性
         * 也就是沖我們自定屬性樣式中,來引用你需要的某條屬性
        */
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.Myview);
        int colors =typedArray.getColor(R.styleable.Myview_rect_color,0xffff0000);//給他賦值一個紅色
        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時指定這條屬性是屬于什么類型-->
    <declare-styleable name="Myview">

        <attr name="rect_color" format="color"></attr>
        <attr name="rect_text" format="reference"></attr>

    </declare-styleable>
</resources>

到此這篇關于圖文詳解自定義View視圖的屬性及引用的文章就介紹到這了,更多相關自定義View視圖屬性引用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

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

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

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

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

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

    Android補間動畫的實現(xiàn)示例

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

    深入探討Unit Testing in Android

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

    詳解Android studio 3+版本apk安裝失敗問題

    這篇文章主要介紹了詳解Android studio 3+版本apk安裝失敗問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • android 手機截取長屏實例代碼

    android 手機截取長屏實例代碼

    本篇文章主要介紹了android 手機截取長屏實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 開源電商app常用標簽

    開源電商app常用標簽"hot"之第三方開源LabelView

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

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

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

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

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

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

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

最新評論

海门市| 罗山县| 哈尔滨市| 和龙市| 滁州市| 涡阳县| 涟源市| 九寨沟县| 灌阳县| 津市市| 崇义县| 云和县| 梅河口市| 阳信县| 沭阳县| 汤原县| 安塞县| 广南县| 佛山市| 垣曲县| 剑阁县| 奉新县| 杭锦后旗| 平塘县| 汉川市| 铅山县| 阳高县| 黄梅县| 泽普县| 甘南县| 昌邑市| 泸西县| 浙江省| 镇平县| 阳江市| 府谷县| 曲沃县| 邵武市| 天门市| 乐至县| 永昌县|