自定义控件如何获取 android:layout_width属性

来源:互联网 发布:手机查经纬度软件 编辑:程序博客网 时间:2024/05/19 23:12

1,新建attr文件


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="album">
        <attr name="android:layout_width"/>
        <attr name="android:layout_height"/>
    </declare-styleable>
</resources>



2,在view构造函数中获取属性值:

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.album, defStyleAttr, 0);
try {
    int width = a.getLayoutDimension(R.styleable.album_android_layout_width, -1);
    int height = a.getLayoutDimension(R.styleable.album_android_layout_height, -2);
    Log.d(TAG, "AlbumView: " + width + " " + height);
} finally {
  a.recycle();
}

0 0
原创粉丝点击