Android自定义属性使用

Android自定义属性使用以及注意事项

自定义控件和自定义属性是安卓开发过程中实现个性化的一个很好技巧,也是对sdk中UI部分的使用更加灵活的部分。这部分主要实现自定义属性,自定义控件的文档正在完善中。

如何实现:

a) 在配置文件中声明。

虽说在value中哪个文件里都是一样(保证在resources标签下就行),但作为一个有志向的开发者,良好的文件管理习惯是必须的,so,在values中创建一个名为:attrs.xml来统一管理自定义属性。

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="ImageTextView">
        <attr name="select_src" format="reference" />
        <attr name="normal_src" format="reference" />
        <attr name="select_text_color" format="color" />
        <attr name="normal_text_color" format="color" />
        <attr name="image_text_padding" format="dimension"/>
        <attr name="android:textSize"/>
        <attr name="android:layout_gravity" />
        <attr name="android:text" />
    </declare-styleable>
</resources>

自定义属性的声明是通过子标签declare-styleable声明了大的样式,具体的属性是通过attr来声明,声明包含属性名和格式(具体是属性值的格式,一般有reference,color,integer,float,dimension,boolean,string等,当然还有enum,如果是枚举的话要继续声明具体的枚举),如果对于自定义控件来说,某些属性就是已经android声明的属性,就可以在name后面直接写明使用android:声明的哪个属性(简单吧)。

b) 在自定义控件xml中使用

(主要是讲解自定义属性,所以这里的自定义控件较为简单,就是一个imageview + textview的组合)

<com.android.lesdo.view.UpImageTextView
	//声明使用app来定义自定义属性名,当然这个命名是你确定的。
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text = "Hello"
    android:textSize = "14sp"
    android:layout_marginRight="59dp"
    // 使用刚才已经在arrts里声明好的属性
    app:select_src = "@drawable/searchuser_t_pressed" 
    app:normal_src = "@drawable/searchuser_t_normal"
    app:select_text_color = "@color/green"
    app:normal_text_color = "@color/content"
    app:image_text_padding = "7dp"
    />

当然,你也可以声明android本来的属性,一样可以使用(具体在自定义控件里说明)。

c) 在自定义控件(java类)中读取属性使用

最后的使用。

自定义控件大多数要继承原本的ImageviewTextViewFrameLayout等,当然直接继承View也是一样的,只不过直接继承View和ViewGroup的灵活性太高,一般用不到。

public UpImageTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    // 加载布局文件
    LayoutInflater.from(context).inflate(R.layout.down_text_image,this);
        imageView = (ImageView) findViewById(R.id.image);
        textView = (TextView) findViewById(R.id.text);

    // 获取属性集
  	// 这里获取的就是我们再res 下value中声明的ImageTextView属性集
  	// 当然是由IDE自动在R文件中生成的。
        final TypedArray a = context.obtainStyledAttributes(
                attrs, R.styleable.ImageTextView);

    // 通过声明的format 就可以定向获取到属性
  	// R.styleable 都是自动生成的,具体属性名都是跟在声明的style名字后面的
  	// R.styleable.ImageTextView_normal_src对应的就是 arrts.xml中的
  	// <attr name="normal_src" format="reference" />

       int selectRes = a.getResourceId(R.styleable.ImageTextView_select_src,0);
       int normalRes = a.getResourceId(R.styleable.ImageTextView_normal_src,0);
       int selectTextColor = a.getResourceId(R.styleable.ImageTextView_select_text_color,R.color.green);
       int normalTextColor = a.getResourceId(R.styleable.ImageTextView_normal_text_color, R.color.title);
      int textSize = a.getDimensionPixelSize(R.styleable.ImageTextView_android_textSize, 0);
        int image_text_padding = a.getDimensionPixelOffset(R.styleable.ImageTextView_image_text_padding,0);
        String text = a.getString(R.styleable.ImageTextView_android_text);

        a.recycle();
        imageView.setImageResource(normalRes);
        textView.setTextColor(getResources().getColor(normalTextColor));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        textView.setText(text);
        textView.setPadding(0,image_text_padding,0,0);
}

依次获取属性,然后使用。弹药注意的是,获取完自定义或者本身的属性后,一定要将final TypedArray a返回到用之前的状态,可以看到TypedArray是context中获取的,它是一个环境中统一使用的,所以用完记得要还。

 a.recycle();

注意的坑:

 int textSize = a.getDimensionPixelSize(R.styleable.ImageTextView_android_textSize, 0);

这里获取的 ,单位是sp得值 ,单位是sp得值 ,单位是sp得值!!!

所以使用的时候记得加上TypedValue

textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
comments powered by Disqus