现在的位置: 首页 > 综合 > 正文

typedarray自定义属性的使用 .

2013年02月06日 ⁄ 综合 ⁄ 共 2903字 ⁄ 字号 评论关闭

转自:http://blog.csdn.net/fuuckwtu/article/details/6522090

 

今天本来在AlertDialog源码还有theme,想修改一下AlertDialog的背景,主题什么的,结果看到一个帖子,讲如何修改AlertDialog的背景,看的我晕乎乎的,很多类不知道,typedarray就是其中一个。

于是我网上找了点资料看看。

我们平常在xml文件里面定义,设置控件属性,android:text android:size 这类的,有木有可以自定义的呢

答案是肯定的。android这个做的还是比较人性化的,可以自定义属性,自定义实现风格,主题,之类的。

首先在values文件下,新建一个attrs.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<resources>

   <declare-styleable name="MyView">
       <attr name="textColor" format="color"/>
       <attr name="textSize" format="dimension"/>
   </declare-styleable>
</resources>

 <declare-styleable >估计是个自定义标签吧。

换了别的不行

然后使用自定义属性来设置view

package cn.edu.wtu;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

 private Paint               mPaint;
 private Context             mContext;
 private static final String mStr ="welcome to typed-array";
 
 public MyView(Context context){
  super(context);
  mPaint = new Paint();
 }
 public MyView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
  mPaint = new Paint();
  TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView);
//  获取属性,0xFFFFFFFF 16是默认值
  int   textColor = a.getColor(R.styleable.MyView_textColor, 0xFFFFFFFF);
  float textSize  = a.getDimension(R.styleable.MyView_textSize, 16);
  mPaint.setTextSize(textSize);
  mPaint.setColor(textColor);
//  回收,为以后再使用
  a.recycle();
 }
 
 protected void onDraw(Canvas canvas){
  super.onDraw(canvas);
  mPaint.setStyle(Style.FILL);
  canvas.drawRect(10, 10, 100, 100, mPaint);
  mPaint.setColor(Color.BLUE);
  canvas.drawText(mStr,10,110,mPaint);
 }
}
 

注意一下R.styleable.MyView,R.styleable.MyView_textColor,这些命名。

在main.xml文件里面使用myview这个控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:test="http://schemas.android.com/apk/res/cn.edu.wtu"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
    >
 <TextView 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/hello"
    />
    <cn.edu.wtu.MyView android:layout_width="fill_parent"
                       android:layout_height="fill_parent"
                       test:textSize="20sp"
                       test:textColor="#ffffffff"
    />
   
</LinearLayout>
PS:xmlns:test="http://schemas.android.com/apk/res/cn.edu.wtu"
cn.edu.wtu是我的包名,是我的学校的域名

test是名字。test:textColor="#ffffffff"
 

package cn.edu.wtu;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;

public class Alert extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

   
}

 

抱歉!评论已关闭.