111_约束子控件宽高
2016-05-13 23:26
190 查看
约束子控件的宽高
刚刚的基础代码
public class FlowLayout extends LinearLayout {public FlowLayout(Context context) {
super(context);
}
public FlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}
}
我们不能让子控件的宽比父控件的宽还大
所以我们要约束子控件的宽//可用的宽和高
int availableWidth = widthSize - getPaddingLeft() - getPaddingRight();
int availableHeight = heightSize - getPaddingTop() - getPaddingBottom();
int childCount = getChildCount();
//约束子控件
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
//重新定义子控件宽高
int widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
int heightSpec = MeasureSpec.makeMeasureSpec(availableHeight, MeasureSpec.AT_MOST);
//新的测量规则生效
childView.measure(widthSpec, heightSpec);
//新的宽高
int childMeasuredWidth = childView.getMeasuredWidth();
in
4000
t childMeasuredHeight = childView.getMeasuredHeight();
//子控件间距
ViewGroup.LayoutParams layoutParams = childView.getLayoutParams();
}
设定子控件的参数
@Overridepublic LayoutParams generateLayoutParams(AttributeSet attrs) {
//返回子控件的LayoutParams
MarginLayoutParams marginLayoutParams = new MarginLayoutParams(getContext(),attrs);
return (LayoutParams) marginLayoutParams;
}
相关文章推荐
- Superprime Rib
- 初探RxJava
- JSON.stringify()、eval(),JSON.parse()各自的区别于作用
- android studio发布版和测试版证书SHA1获取
- 菜刀ASPX 读取文件
- 进程的相关知识
- sql server 提示无法彻底删除_复制-而无法删除数据库或重新配置发布订阅
- 线性表的操作 (1)实现 A= A U B
- 程序员的自我修养 读书笔记01
- 2.网页布局之切切豆腐
- nginx学习笔记之二:nginx作为web server
- 109_飞入飞出效果的集成
- JS学习1简介
- 构建之法阅读笔记03
- 删除对话框练习
- sqlserver修改某列为行号
- (1)Java网络编程----网络基础
- BFS和DFS的差别,BFS实现迷宫最短路径
- 【转】查看java类是从哪个包加载
- 用户描述