有时候android传统的页面布局不足以满足我们的需求,常常需要自己定义view,通常继承View,然后重写构造方法以及onDraw等函数,再具体实现自己定义的复杂view。我们知道在给控件赋属性时,通常使用的是android系统自带的属性,比如 android:layout_height="wrap_content",除此之外,我们亦可以自己定义属性,这样在使用的时候我们就可以使用形如 myapp:myTextSize="sp"的方式了,步骤大致如下:1》在项目文件res/value下面创建一个attr.xml文件,该文件中包含若干个attr集合,例如:[html]<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyView"> <attr name="myTextSize" format="dimension"/> <attr name="myColor" format="color"/> </declare-styleable> </resources> 其中resource是跟标签,可以在里面定义若干个declare-styleable,<declare-styleable name="MyView">中name定义了变量的名称,下面可以再自定义多个属性,针对<attr name="myTextSize" format="dimension"/>来说,其属性的名称为"myTextSize",format指定了该属性类型为dimension,只能表示字体的大小。format还可以指定其他的类型比如;reference 表示引用,参考某一资源IDstring 表示字符串color 表示颜色dimension 表示尺寸boolean 表示布尔integer 表示整型float 表示浮点fraction 表示百分数enum 表示枚举flag 表示位运算 2》在使用到该自定义view的布局文件中键入如下的一行:绿色是自己定义属性的前缀名字,粉色是项目的包名,这样一来,在我们自己定义的view的属性中,就可以使用自己在attr中定义的属性啦,例如:[html] <LinearLayout xmlns:android=" xmlns:myapp=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <com.eyu.attrtextdemo.MyView android:layout_height="wrap_content" android:layout_width="wrap_content" myapp:myTextSize="sp" myapp:myColor="#"/> </LinearLayout> 3》在自定义view的代码中引入自定义属性,修改构造函数context通过调用obtainStyledAttributes方法来获取一个TypeArray,然后由该TypeArray来对属性进行设置obtainStyledAttributes方法有三个,我们最常用的是有一个参数的obtainStyledAttributes(int[] attrs),其参数直接styleable中获得TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);调用结束后务必调用recycle()方法,否则这次的设定会对下次的使用造成影响 具体如下:[java] package com.eyu.attrtextdemo; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Paint.Style; import android.util.AttributeSet; import android.view.View; public class MyView extends View{ public Paint paint; public MyView(Context context, AttributeSet attrs) { super(context, attrs); paint = new Paint(); TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView); int textColor = a.getColor(R.styleable.MyView_myColor, ); float textSize = a.getDimension(R.styleable.MyView_myTextSize, ); paint.setTextSize(textSize); paint.setColor(textColor); a.recycle(); } public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override www.2cto.com protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); paint.setStyle(Style.FILL); canvas.drawText("aaaaaaa", , , paint); } } 运行后:
推荐整理分享Android自定义属性时TypedArray的使用方法(android 自定义dialog),希望有所帮助,仅作参考,欢迎阅读内容。
![Android自定义属性时TypedArray的使用方法(android 自定义dialog)](https://www.jiuchutong.com/image/20240129/1706506851.jpg)
文章相关热门搜索词:android 自定义actionbar,android 自定义dialog,android自定义属性详解,android 自定义,android自定义属性attr,Android自定义属性名相同format不同,android自定义属性详解,Android自定义属性名相同format不同,内容如对您有帮助,希望把文章链接给更多的朋友!
Android使用JNI(从java调用本地函数) 当编写一个混合有本地C代码和Java的应用程序时,需要使用Java本地接口(JNI)作为连接桥梁。JNI作为一个软件层和API,允许使用本地代码调用Java对象的
Android - DrawerLayout的使用 AndroidDrawerLayout的使用。从屏幕的左右边缘开始拖动,能拖出一个新的视图。activity_main.xmlandroid.support.v4.widget.DrawerLayoutxmlns:android=
Android开发:StaggeredGridView瀑布流控件运行异常崩溃解决方法 StaggeredGridView是github上一个开源的瀑布流图片库,本文将分享集成StaggeredGridView时碰到的异常以及解决方法,StaggeredGriedView开源地址为: