typedarray自定义属性的使用

原文连接 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);
    }


    
}

原文连接:http://w11h22j33.iteye.com/blog/890410

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值