android-R.styleable(应用)
来源:互联网 发布:windows 10控制面板 编辑:程序博客网 时间:2024/05/01 09:41
在res/values 文件下定义一个attrs.xml 文件
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="CusView"> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> </declare-styleable></resources>布局文件引用:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cus="http://schemas.android.com/apk/res/com.z.boolattribute" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- com.z.boolattribute 是我们包名 --> <com.z.boolattribute.CusView android:layout_width="fill_parent" android:layout_height="wrap_content" cus:textColor="#ff00cc" cus:textSize="20sp" > </com.z.boolattribute.CusView></LinearLayout>之定义view
package com.z.boolattribute;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.graphics.Rect;import android.util.AttributeSet;import android.view.View;//com.z.boolattribute.CusViewpublic class CusView extends View {private Paint mPaint;private Context mContext;public CusView(Context context) {super(context);mPaint = new Paint();}public CusView(Context context, AttributeSet attrs) {super(context);mPaint = new Paint();TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CusView);// 设置默认值int textColor = array.getColor(R.styleable.CusView_textColor, 0Xff00cc);float textSize = array.getDimension(R.styleable.CusView_textSize, 36);mPaint.setTextSize(textSize);mPaint.setColor(textColor);array.recycle();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);// 设置填充mPaint.setStyle(Style.FILL);// 画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);mPaint.setColor(Color.BLUE);// 绘制文字canvas.drawText("Look Me", 10, 120, mPaint);}}
package com.z.boolattribute;import android.app.Activity;import android.os.Bundle;public class TwoActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_two);}}
demo下载
0 0
- android-R.styleable(应用)
- android-R.styleable
- R.styleable.gallery报错,Android中R.styleable 无法解析时候的解决办法
- Android示例HelloGallery中R.styleable unresolved的解决办法
- Android中R.styleable 无法解析时候的解决办法
- Android示例HelloGallery中R.styleable unresolved的解决办法
- Android中R.styleable 无法解析时候的解决办法
- 【Android】TypedArray和obtainStyledAttributes使用(R.styleable.xx_xx)
- 【转】Android示例HelloGallery中R.styleable unresolved的解决办法
- android 绕过R文件,通过代码获取styleable的属性
- java.lang.NoClassDefFoundError: android.support.v7.recyclerview.R$styleable
- java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable
- Android中R.styleable 无法解析时候的解决办法
- Didn't find class "android.support.v7.cardview.R$styleable"
- java.lang.NoSuchFieldError: android.support.v7.appcompat.R$styleable.Theme_w
- 找不到R.styleable
- android styleable
- r.style r.styleable r.attr
- 数据结构 C语言实现 双向链表的增加与删除节点
- fragment 嵌套
- makefile的语法及写法
- 无法访问linux上已经部署好的项目
- sqlserver md5 加密
- android-R.styleable(应用)
- MyStringUtil.java String工具类
- saiku (branch 3.8-release)构建步骤
- 浅谈Java 多态,向上转型
- mybatis访问数据
- plsql连接oracle数据库
- Linux 两台主机之间建立信任关系方式及基本原理
- 定时器
- 重载求最大值