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

首頁 > 系統(tǒng) > Android > 正文

Android開發(fā)之在xml中設(shè)置自定義屬性的方法

2019-10-22 18:16:56
字體:
供稿:網(wǎng)友

xml中設(shè)置自定義屬性

分三步:

1. 在項(xiàng)目中的values文件中創(chuàng)建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中關(guān)聯(lián)這些屬性</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);//第二個(gè)參數(shù)是設(shè)置的默認(rèn)值,當(dāng)你不設(shè)置這個(gè)屬性時(shí)會使用這個(gè)值   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文件中設(shè)置屬性和命名空間</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是這樣寫的,添加屬性的時(shí)候自動(dòng)生成  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"  //自己的設(shè)置的屬性      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開發(fā)之在xml中設(shè)置自定義屬性的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿城市| 卢龙县| 桦甸市| 关岭| 郑州市| 泽州县| 太谷县| 双柏县| 石狮市| 临漳县| 桦甸市| 凤台县| 依兰县| 大厂| 永胜县| 英山县| 得荣县| 商都县| 屏东市| 孝昌县| 峨眉山市| 涡阳县| 渝北区| 民乐县| 桓台县| 河间市| 汶上县| 永城市| 南雄市| 万州区| 黑河市| 黑山县| 墨江| 高密市| 灵台县| 阿巴嘎旗| 刚察县| 克山县| 德惠市| 唐山市| 丰镇市|