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

首頁 > 系統 > Android > 正文

Android開發之在xml中設置自定義屬性的方法

2019-12-12 01:17:24
字體:
來源:轉載
供稿:網友

xml中設置自定義屬性

分三步:

1. 在項目中的values文件中創建attrs文件

<?xml version="1.0" encoding="utf-8"?> <resources>   <declare-styleable name="QLoadingIndicatorView">      <attr name="indicatorId" format="integer"/>   <attr name="indicatorColor" format="color"/>   <attr name="indicatorText" format="string"/>  </declare-styleable>  </resources> 
<pre name="code" class="java"></pre> <h3><a name="t3"></a>2. 在view中關聯這些屬性</h3> <div><pre name="code" class="java">public class MyView extends LinearLayout {   private int mIndicatorColor,mIndicatorId;  private String mIndicatorText;   public MyView(Context context, AttributeSet attrs, int defStyleAttr) {   super(context, attrs, defStyleAttr);    //從xml的屬性中獲取到值,然后想怎么用就怎么用   TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.QLoadingIndicatorView);   mIndicatorColor=ta.getColor(R.styleable.QLoadingIndicatorView_indicatorColor,Color.BLACK);//第二個參數是設置的默認值,當你不設置這個屬性時會使用這個值   mIndicatorId=ta.getInt(R.styleable.QLoadingIndicatorView_indicatorId,1);   mIndicatorText=ta.getString(R.styleable.QLoadingIndicatorView_indicatorText,"abc");   ta.recycle();   } }</pre><br> <br> </div> <pre name="code" class="java"></pre><pre name="code" class="java"></pre> <h3><a name="t4"></a>3.xml文件中設置屬性和命名空間</h3> <div><pre name="code" class="java"><GridLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  xmlns:app="http://schemas.android.com/apk/res-auto" //這就是自己定義的屬性的命名空間,androidstudio是這樣寫的,添加屬性的時候自動生成  android:rowCount="9"  android:columnCount="4">  <com.zxq.com.myrecycleview.progressbaranimation.QLoadingIndicatorView   android:layout_width="50dp"   android:layout_height="50dp"   android:layout_margin="10dp"   app:indicatorId="0"  //自己的設置的屬性      app:indicatorColor="#F00"   >  </com.zxq.com.myrecycleview.progressbaranimation.QLoadingIndicatorView> </GridLayout></pre><br> <br> </div> <pre name="code" class="java"> <pre></pre> <pre></pre> <pre></pre> <pre></pre> </pre> 

以上這篇Android開發之在xml中設置自定義屬性的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 原平市| 南郑县| 资源县| 金川县| 大港区| 闽清县| 云南省| 新野县| 宣武区| 莱西市| 汉源县| 个旧市| 阿拉善左旗| 苏州市| 宿迁市| 武川县| 阿城市| 莱州市| 桦川县| 丁青县| 烟台市| 井研县| 扎鲁特旗| 琼海市| 满城县| 宝兴县| 称多县| 灵石县| 道孚县| 邹平县| 惠州市| 电白县| 沁阳市| 安乡县| 信阳市| 和龙市| 开远市| 宝鸡市| 容城县| 拉萨市| 威信县|