您的位置:首页 > 其它

TypedArray的用法

2015-07-29 11:08 351 查看
自定义view的属性方式如下::::

1. 在res/values 文件下定义一个attrs.xml 文件.

<resources>

<declare-styleable name="desktopmanager">

<attr name="windowwidth" format="dimension"/>

<attr name="windowheight" format="dimension"/>

<attr name="listwidth" format="dimension"/>

</declare-styleable>

</resources>

2. viewGroup1中自定义viewGroup2的大小

在viewGroup1中代码如下:

//使用“名字_属性"的方法获取

int mWindowHeight = (int) a.getDimension(R.styleable.desktopmanager_windowheight,
1000);

int mWindowWidth = (int) a.getDimension(R.styleable.desktopmanager_windowwidth, 1200);

//在viewGroup1中设置view的实际大小

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int width = MeasureSpec.getSize(widthMeasureSpec);

int height = MeasureSpec.getSize(heightMeasureSpec);

//在viewGroup1中设置viewGroup2的大小

viewGroup2.measure(mWindowWidth | MeasureSpec.EXACTLY, mWindowHeight | MeasureSpec.EXACTLY);

this.setMeasuredDimension(width, height);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: