Android自定义属性的使用示例
2013-09-02 16:00
405 查看
MainActivity如下:
ViewSubclass如下:
main.xml如下:
attrs.xml如下:
package cc.testattrs; import android.os.Bundle; import android.app.Activity; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
ViewSubclass如下:
package cc.testattrs; 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; /** * Demo描述: * Android自定义属性的使用 * * 注意事项: * 1 在main.xml中声明命名空间 * xmlns:testattr="http://schemas.android.com/apk/res/cc.testattrs" * 其中http://schemas.android.com/apk/res/为固定写法,其后追加包名 * testattr为我们给自定义属性的别名引用 * 2 getDimension(R.styleable.TestAttr_testTextSize, 20); * 第二个参数意思是:假如在xml文件中没有为改属性设值则采用此值. * 其余getXX()方法均类似 * 3 注意getColor()方法中第二个参数的取值,是一个颜色值,在这里很容易错误 * */ public class ViewSubclass extends View { private Paint mPaint; private float textSize; private int textColor ; public ViewSubclass(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ViewSubclass(Context context, AttributeSet attrs) { super(context, attrs); mPaint = new Paint(); TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.TestAttr); textSize = typedArray.getDimension(R.styleable.TestAttr_testTextSize, 20); textColor = typedArray.getColor(R.styleable.TestAttr_testColor, Color.BLACK); System.out.println("textSize="+textSize+",textColor="+textColor); mPaint.setTextSize(textSize); mPaint.setColor(textColor); //切记recycle() typedArray.recycle(); } public ViewSubclass(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setStyle(Style.FILL); canvas.drawText("9527", 10, 20, mPaint); } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:testattr="http://schemas.android.com/apk/res/cc.testattrs" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <cc.testattrs.ViewSubclass android:layout_width="200dip" android:layout_height="200dip" android:layout_centerInParent="true" testattr:testTextSize="10dip" testattr:testColor="#ff0000" /> </RelativeLayout>
attrs.xml如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TestAttr"> <attr name="testTextSize" format="dimension"/> <attr name="testColor" format="color"/> </declare-styleable> </resources>
相关文章推荐
- Android自定义属性的使用示例
- Android中属性动画Property Animation使用示例(一)
- android在style中使用自定义属性 error: style attribute not found.
- Android种使用Notification实现通知管理以及自定义通知栏实例(示例四)
- Android 自定义View使用示例(三)
- Android自定义属性--------TypedArray和obtainStyledAttributes使用
- android-在开发中使用自定义属性名
- 在android style文件中使用自定义属性
- 说说Android 两种为自定义组件添加属性的使用方法和区别
- Android自定义属性时TypedArray的使用方法
- 说说Android 两种为自定义组件添加属性的使用方法和区别
- 说说Android 两种为自定义组件添加属性的使用方法和区别
- Android自定义View时使用TypedArray配置样式属性
- Android中属性动画Property Animation使用示例(一)
- Android自定义属性时TypedArray的使用方法
- Android自定义属性时TypedArray的使用方法
- Android 对Layout_weight属性完全解析以及使用ListView来实现表格(自定义适配器)
- 在android style.xml文件中使用自定义属性
- android studio自定义属性使用
- 继承android.view.View自定义view,使用画笔绘制view示例