亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解Android自定義控件屬性

 更新時間:2016年02月17日 11:15:46   作者:BetterLaterThanNever  
這篇文章主要為大家詳細(xì)介紹了Android自定義控件屬性,需要的朋友可以參考下

在Android開發(fā)中,往往要用到自定義的控件來實現(xiàn)我們的需求或效果。在使用自定義
控件時,難免要用到自定義屬性,那怎么使用自定義屬性呢?

在文件res/values/下新建attrs.xml屬性文件,中定義我們所需要的屬性。

<?xml version="1.0" encoding="utf-8"?>

<resources><!-- resource是跟標(biāo)簽,可以在里面定義若干個declare-styleable -->
  <declare-styleable name="custom_view"><!-- name定義了變量的名稱 -->
    <attr name="custom_color" format="color"></attr> <!-- 定義對應(yīng)的屬性,name定義了屬性的名稱 -->
    <attr name="custom_size" format="dimension"></attr> <!--每一個發(fā)生要定義format指定其類型,類型包括 
     reference  表示引用,參考某一資源ID
     string  表示字符串
     color  表示顏色值
     dimension  表示尺寸值
     boolean  表示布爾值
     integer  表示整型值
     float  表示浮點值
     fraction  表示百分?jǐn)?shù)
     enum  表示枚舉值
     flag  表示位運算
    -->
 </declare-styleable>

public class CustomTextView extends TextView {
  private int textSize;//自定義文件大小
  private int textColor;//自定義文字顏色

  //自定義屬性,會調(diào)用帶兩個對數(shù)的構(gòu)造方法
  public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.custom_view);//TypedArray屬性對象
    textSize = ta.getDimensionPixelSize(R.styleable.custom_view_custom_size, 20);//獲取屬性對象中對應(yīng)的屬性值 
    textColor = ta.getColor(R.styleable.custom_view_custom_color, 0x0000ff);
    setColorAndSize(textColor, textSize);//設(shè)置屬性
    ta.recycle();
  }

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

  private void setColorAndSize(int textColor, int textSize) {
    setTextColor(textColor);
    setTextSize(textSize);
  }

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:ldm="http://schemas.android.com/apk/res/com.ldm.learn"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#f6f6f6"
  android:orientation="vertical"
  android:padding="10dp" >

  <com.ldm.learn.CustomTextView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:text="自定義TextView"
    ldm:custom_color="#333333"
    ldm:custom_size="35sp" />

</LinearLayout>

布局說明:


通過以上幾步就可以實現(xiàn)我們想要的自定義屬性效果(用自定義屬性設(shè)置文字大小及顏色)啦!

相關(guān)文章

  • Android監(jiān)聽橫豎屏切換功能

    Android監(jiān)聽橫豎屏切換功能

    在這篇文章主要介紹了Android監(jiān)聽橫豎屏切換功能,非常不錯,代碼簡單易懂,具有參考借鑒價值,需要的朋友可以參考下
    2018-02-02
  • Android Service綁定過程完整分析

    Android Service綁定過程完整分析

    這篇文章主要為大家詳細(xì)介紹了Android Service綁定完整過程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • android在異步任務(wù)中關(guān)閉Cursor的代碼方法

    android在異步任務(wù)中關(guān)閉Cursor的代碼方法

    android在異步任務(wù)中如何關(guān)閉Cursor?在我們開發(fā)應(yīng)用的時候,很多時候會遇到這種問題,下面我們就看看代碼如何實現(xiàn)
    2013-11-11
  • 簡單實現(xiàn)Android讀取網(wǎng)絡(luò)圖片到本地

    簡單實現(xiàn)Android讀取網(wǎng)絡(luò)圖片到本地

    這篇文章主要為大家詳細(xì)介紹了如何簡單實現(xiàn)Android讀取網(wǎng)絡(luò)圖片到本地的方法,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android中外接鍵盤的檢測的實現(xiàn)

    Android中外接鍵盤的檢測的實現(xiàn)

    這篇文章主要介紹了Android中外接鍵盤的檢測的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • android?WindowManager的簡單使用實例詳解

    android?WindowManager的簡單使用實例詳解

    這篇文章主要介紹了android?WindowManager的簡單使用,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • Android開發(fā)筆記之:AsyncTask的應(yīng)用詳解

    Android開發(fā)筆記之:AsyncTask的應(yīng)用詳解

    本篇文章是對Android中AsyncTask的應(yīng)用進行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Android自定義實現(xiàn)轉(zhuǎn)盤菜單

    Android自定義實現(xiàn)轉(zhuǎn)盤菜單

    旋轉(zhuǎn)菜單是一種占用空間較大,實用性稍弱的UI,本文主要為大家詳細(xì)介紹了Android如何自定義實現(xiàn)轉(zhuǎn)盤菜單,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下
    2023-12-12
  • Android持久化技術(shù)之文件的讀取與寫入實例詳解

    Android持久化技術(shù)之文件的讀取與寫入實例詳解

    這篇文章主要介紹了Android持久化技術(shù)之文件的讀取與寫入操作,結(jié)合實例形式較為詳細(xì)的分析講述了Android持久化操作的相關(guān)技巧與具體實現(xiàn)方法,需要的朋友可以參考下
    2016-01-01
  • Android編程實現(xiàn)點擊鏈接打開APP功能示例

    Android編程實現(xiàn)點擊鏈接打開APP功能示例

    這篇文章主要介紹了Android編程實現(xiàn)點擊鏈接打開APP功能,結(jié)合實例形式較為詳細(xì)的分析了Android實現(xiàn)點擊鏈接打開APP功能的具體步驟與相關(guān)注意事項,需要的朋友可以參考下
    2017-01-01

最新評論