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

Android如何自定義視圖屬性

 更新時(shí)間:2016年08月02日 17:14:42   作者:u011771755  
這篇文章主要為大家介紹了Android如何自定義視圖屬性,三個(gè)步驟自定義視圖屬性,感興趣的小伙伴們可以參考一下

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

1. 自定義一個(gè)自己的視圖類繼承自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);
    //必須手動(dòng)回收ta
    ta.recycle();
  }

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

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

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

一個(gè)attr屬性,對(duì)應(yīng)了一個(gè)視圖屬性

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  //自定義一個(gè)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"
    //根據(jù)自定義的命名空間和我們?cè)赼ttrs中設(shè)置的屬性,自定義屬性值
    gu:rect_color="#cc99cc" />
</LinearLayout>

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

阳春市| 扎囊县| 来凤县| 青州市| 汝城县| 桑日县| 新化县| 马鞍山市| 宜宾县| 孙吴县| 天气| 望奎县| 兴义市| 宁化县| 彰化市| 荣昌县| 阿鲁科尔沁旗| 武鸣县| 壤塘县| 尚志市| 关岭| 海门市| 年辖:市辖区| 顺平县| 万山特区| 娄烦县| 洪泽县| 龙门县| 大洼县| 山东省| 东莞市| 榆林市| 九龙城区| 山西省| 依兰县| 崇明县| 明光市| 泉州市| 无为县| 延寿县| 大同市|