热门搜索 :
考研考公
您的当前位置:首页正文

Android自定义属性的理解

来源:东饰资讯网

一个Android开发者总会遇到自定义控件的问题。要学会自定义控件的开发,最好的方法是将要用到的知识点一个个掌握。当掌握这些分散的知识点就意味着写一个自定义控件会变得容易。本篇文章是对自定义属性的探究。

a、如何自定义属性
在res/values中的attrs.xml中自定义属性。

<declare-styleable name="TestView">    
   <attr name="attrone" format="dimension"/>    
   <attr name="attrtwo" format="string" >    
    <enum name="one" value="0"/>    
    <enum name="two" value="1"/>
   </attr>
</declare-styleable>

分析一下以上代码代表的含义:
declare-styleable: 表示一个属性组。它的name必须和你自定义view的名字相同。
attr:表示单独的一个属性。format代表属性的格式。格式包括很多种:比如颜色,数值,枚举等。 看下图:


formart属性
<declare-styleable name="ViewGroup_Layout">
   <attr name="layout_width" format="dimension">
     <enum name="fill_parent" value="-1" /> 
     <enum name="match_parent" value="-1" /> \
     <enum name="wrap_content" value="-2" /> 
  </attr> 
</declare-styleable>

通过以上的源码和实际经验我们知道。 给android:layout_width赋值时可以指定大小比如:10dp;也可以使用默认值:match_parent,wrap_content,fill_parent(这种已经不推荐使用)。这三个值在attr中已经定义好了。可以直接使用;

通过命名空间就可以使用自定义属性了。

<com.mg.axe.androiddevelop.view.TestView   
   android:layout_width="match_parent"    
   android:layout_height="match_parent"    
   app:attrone="10dp"   
   app:attrtwo="two"    />

c、如何获取自定义属性 ?
通过getContext().obtainStyledAttributes()获取TypedArray,
通过TypedArray来获取自定义属性的值。上代码:

attrs中定义的自定义属性:

<declare-styleable name="TestView">    
  <attr name="attrone" format="dimension"/>    
  <attr name="attrtwo" format="string" >    
    <enum name="one" value="0"/>    
    <enum name="two" value="1"/>
  </attr>
</declare-styleable>

布局文件中的使用:
将attrone设为10dp
将attrtwo设为默认值“two”对应的value为1

<LinearLayout 
  
     
  android:layout_width="match_parent"    
  android:layout_height="match_parent"    
  android:orientation="vertical">
  <com.mg.axe.androiddevelop.view.TestView   
     android:layout_width="match_parent"    
     android:layout_height="match_parent"    
     app:attrone="10dp"   
     app:attrtwo="two"    />
</LinearLayout>

获取,并通过log打印出获取的值:

 public class TestView extends View{
   public TestView(Context context) {
     this(context,null);
   }

   public TestView(Context context, AttributeSet attrs) {
      this(context, attrs,0);
   }

   public TestView(Context context, AttributeSet attrs, int defStyleAttr) {
     super(context, attrs, defStyleAttr);
     TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.TestView);
     float attrone = ta.getDimension(R.styleable.TestView_attrone,0);
     Log.i("attrone's value",String.valueOf(attrone));
     String attrTwo = ta.getString(R.styleable.TestView_attrtwo);
     Log.i("attrTwo's value",attrTwo);

     / /测试代码
     Log.i("attr's value",dp2px(10)+"");
  ta.recycle();
 }
//测试代码
 protected int dp2px(int dpval){
   return (int) 
 }
}
10-08 18:07:39.536 6313-6313/com.mg.axe.androiddevelop I/attrone'svalue: 30.0
10-08 18:07:39.536 6313-6313/com.mg.axe.androiddevelop I/attrTwo'svalue: 1

10-08 18:07:39.536 6313-6313/com.mg.axe.androiddevelop I/attr'svalue: 30
public float getDimension(int index, float defValue) {
 if (mRecycled) {
   throw new RuntimeException("Cannot make calls to a recycled instance!");
 }
 index *= AssetManager.STYLE_NUM_ENTRIES;
 final int[] data = mData;
 final int type = data[index+AssetManager.STYLE_TYPE];
 if (type == TypedValue.TYPE_NULL) {
   return defValue;
 } else if (type == TypedValue.TYPE_DIMENSION) {
   return  + AssetManager.STYLE_DATA], mMetrics);
 } else if (type == TypedValue.TYPE_ATTRIBUTE) {
   final TypedValue value = mValue;
   getValueAt(index*AssetManager.STYLE_NUM_ENTRIES, value);
   throw new UnsupportedOperationException( "Failed to resolve attribute at index " + index + ": " + value);
 }
 throw new UnsupportedOperationException("Can't convert to dimension: type=0x"
 + Integer.toHexString(type));
}

2、attr定义的enum和flag的value必须是数字。否则无法编译通过,类似于以下错误:


编译无法通过的Log
Top