Android自定义View属性,使用或获取自定义View属性,获取View默认属性

> 获取自定义View属性
Android 自定义属性时TypedArray的使用- https://blog.csdn.net/cswhale/article/details/53100792
 TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomMenu, defStyleAttr, 0);
 int textColor = typedArray.getColor(R.styleable.MyTextView_color, Color.BLACK);
 int radius = typedArray.getDimensionPixelOffset(R.styleable.CircleView_radius, 20);
 int width= typedArray.getInt(R.styleable.burce_mWidth,0);
 String name= typedArray.getString(R.styleable.burce_mName);

> 使用自定义View属性
Android自定义View获取自定义属性- https://blog.csdn.net/leilifengxingmw/article/details/70234052
自定义View属性- https://blog.csdn.net/tangxueqin/article/details/79219738
1) attr.xml  <?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="mybitmapview2">
        <attr name="bitnapview_paintwidth" format="dimension" ></attr>
        <attr name="bitmapbackground"  format="reference"></attr>   
    </declare-styleable>
</resources>

2) <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mybitmapview2="http://schemas.android.com/apk/res/com.example.myview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button 
       android:id="@+id/btn_bitmapview2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="生成图片"
       mybitmapview2:bitnapview_paintwidth="100dp" />

   <com.example.myview.MyBitmapView2
        android:id="@+id/bitmap2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        mybitmapview2:bitmapbackground="@drawable/aln"/>
</LinearLayout>

< attr name= "middleIv" format ="integer" />
<attr name="color" format="color"/> 注意:refrence , 表示attr取值只能是一个指向资源的id
自定义属性中 name是自定义属性的名字,format是指自定义属性的取值。format的取值类如下所示:
  1)  string:字符串类型;string 表示attr取值是String类型,或者一个指向String的资源id,例如R.string.testString
  2)  integer:整数类型;integer 表示attr取值是整型
  3)  float:浮点型;float 表示attr取值是整形或者浮点型
  4)  dimension:尺寸,后面必须跟dp、dip、px、sp等单位;dimension 表示 attr 取值是尺寸类型,例如例如取值16sp、16dp,也可以是一个指向dimen的资源id,例如R.dimen.dp_16
  5)  Boolean:布尔值;boolean 表示attr取值为true或者false
  6)  reference:引用类型,传入的是某一资源的ID,必须以“@”符号开头;reference 表示attr取值只能是一个指向资源的id。
  7)  color:颜色,必须是“#”符号开头;color 表示attr取值是颜色类型,例如#ff3344,或者是一个指向color的资源id,例如R.color.colorAccent.
  8)  fraction:百分比,必须是“%”符号结尾;fraction 表示 attr取值是百分数类型,只能以%结尾,例如30%
  9)  enum:枚举类型;enum 表示attr取值只能是枚举类型。

  10) flag 表示attr取值是flag类型。

> View重写onMeasure()方法;重写onDraw()方法;重写onLayout()方法;
onMeasure()会在初始化之后调用一到多次来测量控件或其中的子控件的宽高;
onLayout()会在onMeasure()方法之后被调用一次,将控件或其子控件进行布局;
onDraw()会在onLayout()方法之后调用一次,也会在用户手指触摸屏幕时被调用多次,来绘制控件。

> 获取系统View属性,View默认属性
-- View默认属性
view属性详解- https://blog.csdn.net/xuegangic/article/details/56671017
Android布局_View属性- https://blog.csdn.net/qq_26544283/article/details/52345748
Android View 属性大全- https://blog.csdn.net/qq_27061049/article/details/79821105

-- 获取View默认属性
//首先通过obtainStyledAttributes获取TypedArray
TypedArray a = theme.obtainStyledAttributes(attrs,
  com.android.internal.R.styleable.TextViewAppearance, defStyleAttr, defStyleRes); 
//接着从TypedArray中提取相应的值,此处提取的是resourceId,对于的attr声明应为“reference”        
//除此之外,TypedArray还有getInt,getDrawable等一系列方法用于提取其他类型的值。
int ap = a.getResourceId(
                com.android.internal.R.styleable.TextViewAppearance_textAppearance, -1);
//回收TypedArray
a.recycle();

privatestaticfinalint[] styleable =newint[] { 
  android.R.attr.layout_width, // index 0 
  android.R.attr.layout_height, // 1
  android.R.attr.layout_margin, // 2 
  android.R.attr.padding // 3};

TypedArray array = context.obtainStyledAttributes(attrs, styleable);
int n = array.getIndexCount();
for(inti =0; i < n; i++) {
  int index = array.getIndex(i);
  int value = array.getDimensionPixelSize(index,0);
}
array.close();
 经测试,这个方法 android:padding是读取不到(有待测试),所以并不推荐。

-- 通过Android的反射机制实现系统属性的设置和获取
public static void set(String prop, String value) {
    try {
        if (null == set) {
            synchronized (PropertyUtils.class) {
                if (null == set) {
                    Class<?> cls = Class.forName("android.os.SystemProperties");
                    set = cls.getDeclaredMethod("set", new Class<?>[]{String.class, String.class});
                }
            }
        }
        set.invoke(null, new Object[]{prop, value});
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

public static String get(String prop, String defaultvalue) {
    String value = defaultvalue;
    try {
        if (null == get) {
            synchronized (PropertyUtils.class) {
                if (null == get) {
                    Class<?> cls = Class.forName("android.os.SystemProperties");
                    get = cls.getDeclaredMethod("get", new Class<?>[]{String.class, String.class});
                }
            }
        }
        value = (String) (get.invoke(null, new Object[]{prop, defaultvalue}));
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return value;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值