AndroidStudio 视图自定义属性
来源:互联网 发布:域名注册证书下载 编辑:程序博客网 时间:2024/05/17 03:01
. 创建 MyRect 类 继承View ,这样就可以 在layout 下的XML 文件中定义;
/**
* 如果我们想自定义视图的话,我们得需要创建一个类继承自View,用View呈现一个长方形
*/
public class MyRext extends View {
//这个构造方法是资源解析程序使用的使用的 -只要把组件放到xml文件里面就会执行这个方法。
//它有一个返回值,TypedArray .使用obtainStyledAttributes把attrs传进来第二个参数是所绑定的View
public MyRext(Context context,@NullableAttributeSet attrs) {
super(context,attrs);
System.out.println("走了这一句");
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.MyView);
setBackgroundColor(ta.getColor(R.styleable.MyView_rect_color,0xffff0000));
ta.recycle();
}
//这个构造方法是代码进行使用的。
public MyRext(Context context) {
super(context);
}
}
. 自定义的MyRect 的视图 布局。
<com.wxb.wxb.customview.MyRext
android:layout_width="100dp"
android:layout_height="100dp"
wxb:rect_color = "#ff0000ff"
/>
.增加 视图的属性,可以在values 下 新建xml 文件。
<?xml version="1.0"encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="rect_color"format="color"/>
</declare-styleable>
</resources>
. 给MyRect 赋值 ,需要写一个自定义的命名空间,
<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:wxb="http://schemas.android.com/apk/res/com.wxb.wxb.customview"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.wxb.wxb.customview.MainActivity">
<com.wxb.wxb.customview.MyRext
android:layout_width="100dp"
android:layout_height="100dp"
wxb:rect_color ="#ff0000ff"
/>
</LinearLayout>
- AndroidStudio 视图自定义属性
- androidStudio引入自定义属性
- AndroidStudio自定义属性无法识别
- unity3d 自定义属性视图
- 自定义视图属性
- android:自定义视图属性
- Android视图自定义属性
- android---自定义视图属性
- AndroidStudio Gradle自定义属性xmlns无法识别
- AndroidStudio中自定义属性及使用
- android自定义视图属性学习
- Android自定义视图和属性
- android 自定义视图控件属性
- 自定义控件,自定义视图状态,复杂属性
- Android自定义视图与自定义属性
- Android开发_自定义视图和属性
- Android如何自定义视图属性总结
- AndroidStudio目录结构视图解说,androidstudio视图
- 【Spring】后端解决跨域问题
- uva 1593 Alignment of Code
- Android硬件访问服务-HAL
- 神经网络NN算法
- php怎么写分页?怎么灵活实现分页的方法,快速在thinkphp里面实现分页
- AndroidStudio 视图自定义属性
- 【easyUI】combobox显示对象内容
- 深入理解事件
- Toast多次弹出的解决办法,随时取消已经弹出的Toast
- elasticsearch 常用接口
- MFC 编辑框控制只输入小数,并控制小数位数,整数位数,值
- 互金平台如何利用大数据做用户留存?
- base64格式验证
- c++多态下的继承