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

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

 更新時(shí)間:2018年01月16日 15:20:56   作者:尒洏強(qiáng)汏  
下面小編就為大家分享一篇Android開發(fā)之在xml中設(shè)置自定義屬性的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

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í)會(huì)使用這個(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è)置自定義屬性的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

景洪市| 绥芬河市| 神农架林区| 宁远县| 阿勒泰市| 承德县| 乃东县| 师宗县| 海原县| 贵德县| 夏津县| 托克托县| 北辰区| 青龙| 连平县| 江安县| 汝城县| 兴城市| 苏州市| 梁河县| 峡江县| 台南市| 昌乐县| 佛学| 南溪县| 肇源县| 蒙自县| 东阿县| 若羌县| 乐陵市| 沧州市| 老河口市| 共和县| 永吉县| 岑巩县| 湖州市| 于田县| 东丰县| 金溪县| 宜城市| 连山|