Android自定义布局属性
来源:互联网 发布:换手率短线炒股软件 编辑:程序博客网 时间:2024/06/01 09:16
需要在values下定义attrs.xml
<declare-styleable name="myStyleable">
<attr name="max" format="integer"/>
<attr name="progress" format="integer"/>
<attr name="textSize" format="dimension"/>
<attr name="smallBgColor" format="color"/>
<attr name="progressColor" format="color"/>
</declare-styleable>
其中fomat表示属性的取值类型
integer:整数
dimension:尺寸 dp dip sp px
color:颜色
string:字符串
float:浮点数 android:rating="1.5"
referrence:引用 @id/btn @drawable/pic
解析属性
public XykjProgressBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
//解析自定义属性
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myStyleable);
textSize = ta.getDimensionPixelSize(R.styleable.myStyleable_textSize, 0);
max = ta.getInteger(R.styleable.myStyleable_max,100);
progress = ta.getInteger(R.styleable.myStyleable_progress,0);
smallBgColor = ta.getColor(R.styleable.myStyleable_smallBgColor,0xff3F51B5);
progressColor = ta.getColor(R.styleable.myStyleable_progressColor,0xffFF4081);
//释放资源
ta.recycle();
}
使用属性
需要在布局中声明前缀的命名空间
xmlns:app="http://schemas.android.com/apk/res-auto"
<com.xykj.myprogressbar.XykjProgressBar
android:id="@+id/progress_bar"
android:layout_width="200dp"
android:layout_height="200dp"
app:textSize="30sp"
app:smallBgColor="#ff000000"/>
<declare-styleable name="myStyleable">
<attr name="max" format="integer"/>
<attr name="progress" format="integer"/>
<attr name="textSize" format="dimension"/>
<attr name="smallBgColor" format="color"/>
<attr name="progressColor" format="color"/>
</declare-styleable>
其中fomat表示属性的取值类型
integer:整数
dimension:尺寸 dp dip sp px
color:颜色
string:字符串
float:浮点数 android:rating="1.5"
referrence:引用 @id/btn @drawable/pic
解析属性
public XykjProgressBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
//解析自定义属性
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myStyleable);
textSize = ta.getDimensionPixelSize(R.styleable.myStyleable_textSize, 0);
max = ta.getInteger(R.styleable.myStyleable_max,100);
progress = ta.getInteger(R.styleable.myStyleable_progress,0);
smallBgColor = ta.getColor(R.styleable.myStyleable_smallBgColor,0xff3F51B5);
progressColor = ta.getColor(R.styleable.myStyleable_progressColor,0xffFF4081);
//释放资源
ta.recycle();
}
使用属性
需要在布局中声明前缀的命名空间
xmlns:app="http://schemas.android.com/apk/res-auto"
<com.xykj.myprogressbar.XykjProgressBar
android:id="@+id/progress_bar"
android:layout_width="200dp"
android:layout_height="200dp"
app:textSize="30sp"
app:smallBgColor="#ff000000"/>
阅读全文
0 0
- Android自定义布局属性
- Android自定义布局属性
- android自定义XML布局属性
- Android自定义属性百分比布局
- xmlns:android作用以及自定义布局属性
- xmlns:android作用以及自定义布局属性
- Android布局文件.xml中的自定义属性
- xmlns:android作用以及自定义布局属性
- xmlns:android作用以及自定义布局属性
- xmlns:android作用以及自定义布局属性
- android 在布局中使用自定义属性
- Android在xml布局中找不到自定义属性的问题
- Android 布局属性详解
- android布局属性详解
- android 布局属性
- Android布局属性详解
- android布局属性详解
- android布局属性详解
- Go编程基础—格式化
- C语言和C++的一些区别
- 部署web项目时tomcat出现 Exception in thread "main"错误
- 利用反射机制来获取表单请求值以及对象赋值(使用了第三方插件)
- [Leetcode]_34 Search for a Range
- Android自定义布局属性
- 洛谷1008
- laravel 模型关联 的简单使用方法
- Android进程通讯
- 大数据R安装依赖库
- SpringMVC数据转换
- 八幅漫画理解使用JSON Web Token设计单点登录系统
- C++使用STL中map需要注意的地方
- Android自定义ViewGroup