国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android如何自定義視圖屬性

2019-12-12 05:49:36
字體:
來源:轉載
供稿:網友

本文實例為大家介紹了Android自定義視圖屬性的方法,供大家參考,具體內容如下

1. 自定義一個自己的視圖類繼承自View

public class MyView extends View{  public MyView(Context context, AttributeSet attrs)  {    super(context, attrs);    //獲取到自定義的屬性    TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.MyView);    int color=ta.getColor(R.styleable.MyView_rect_color, 0xffff0000);    setBackgroundColor(color);    //必須手動回收ta    ta.recycle();  }  public MyView(Context context)  {    super(context);  }}

2. 在res/values目錄中新建一個attrs.xml文件

<?xml version="1.0" encoding="utf-8"?><resources>  //定義一個declare-styleable標簽,在里面設置attr屬性  <declare-styleable name="MyView">    <attr name="rect_color" format="color"/>  </declare-styleable></resources>

一個attr屬性,對應了一個視圖屬性

3.最后看布局文件中如何利用我們創建的自定義視圖并設置其屬性

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  //自定義一個MyView的命名空間  xmlns:gu="http://schemas.android.com/apk/res/com.gu.myrect"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical" >  <com.gu.myrect.MyView    android:layout_width="100dp"    android:layout_height="100dp"    //根據自定義的命名空間和我們在attrs中設置的屬性,自定義屬性值    gu:rect_color="#cc99cc" /></LinearLayout>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 梨树县| 财经| 措美县| 砚山县| 额敏县| 蒲江县| 武安市| 宝兴县| 疏勒县| 乌鲁木齐县| 福州市| 秦安县| 米泉市| 靖西县| 浦北县| 于都县| 广宗县| 纳雍县| 忻州市| 普格县| 达尔| 修水县| 彰化市| 福州市| 凤凰县| 乳源| 洛浦县| 广西| 英吉沙县| 东乌珠穆沁旗| 高邮市| 高尔夫| 汶上县| 南陵县| 辽源市| 淅川县| 武功县| 若尔盖县| 合川市| 三都| 治县。|