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);
}
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);
}
相关文章推荐
- Term::ANSIColor 使用
- GCD理解(一)
- HTML DOCTYPE 重要性
- 【linux高级程序设计】(第十一章)System V进程间通信 1
- Java如何实现图片裁剪预览功能
- Web应用性能优化黄金法则
- 安装Zabbix-Agent yum方式
- 原生Js封装的产品图片360度展示
- 玩转Android---UI篇---GridView(网格布局)
- HDU5317 RGCDQ 质因子分解
- 不固定宽和高的div 水平和垂直都居中
- 内存对齐的影响
- 如何查看我的订单-REST的流程API设计案例
- inux查看进程的所有子进程和线程
- java访问控制的理解
- HDOJ 2120 Ice_cream's world I(并查集判断成环)
- 使用PhoneGap打包HTML成iOS应用
- hdu1203
- java Map对象排序demo
- URAL 1934 最短路变形