在自定义view中获取android layout_width等属性值

来源:互联网 发布:kmeans聚类算法python 编辑:程序博客网 时间:2024/06/06 18:51

这里以获取layout_width和layout_height为例

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