typedarray自定义属性的使用
来源:互联网 发布:计算机算法有什么 编辑:程序博客网 时间:2024/06/05 02:16
今天本来在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); } }
- typedarray自定义属性的使用
- typedarray自定义属性的使用
- typedarray自定义属性的使用
- Android------typedarray自定义属性的使用
- 自定义属性TypedArray的使用步骤
- Android 自定义属性时TypedArray的使用
- Android中的自定义view和自定义属性TypedArray的使用
- 自定义控件的属性(attrs.xml,TypedArray)的使用
- 自定义控件的属性(attr.xml,TypedArray)的使用
- Android中自定义属性(attrs.xml,TypedArray)的使用
- Android 中自定义属性(attr.xml,TypedArray)的使用
- Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android中自定义属性(attrs.xml,TypedArray的使用)
- Android 中自定义属性(attr.xml,TypedArray)的使用!
- Android 中自定义属性(attr.xml,TypedArray)的使用
- Android中自定义属性(attrs.xml,TypedArray的使用)
- Android 中自定义属性(attr.xml,TypedArray)的使用
- 网络5——传输层
- mysql上课笔记(一)
- Linux内核配置之图形化的配置make xconfig
- Bookmarks_2012_06_13
- 基于winpcap的NDIS在IE浏览器,360安全浏览器hack"www.360.cn"
- typedarray自定义属性的使用
- 第二个iPhone应用程序:“Say Hello”
- flashplayer_10_ax_debug10.2.exe 安装错误。您尝试安装的adobeflasplayer不是最新版本
- ANS.1 JAVA和.net的编解码库--BinaryNotes
- Linux网络协议栈之设备初始化
- VS 2008代码自动提示
- oop原则
- C#编程中的66个好习惯
- 菲丝子集