sri*_*san 6 android android-custom-view viewgroup
我有两个习惯viewgroups,superViewGroup和subViewGroup.子视图组包含视图.我将我的superviewgroup添加到linearLayout和subViewGroups我的superviewgroup.
onMeasure()superviewgroup被调用但不在子视图组中.但是在这两种情况下onLayout()方法都被调用了.
代码如下
public class SuperViewGroup extends ViewGroup{
public SuperViewGroup(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.i("boxchart","INSIDE ON MEASURE SUPER VIEWGROUP");
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
child.layout(0, 0, getWidth(), getHeight());
}
}
}
}
public class SubViewGroup extends ViewGroup{
public SubViewGroup(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.i("boxchart","INSIDE ON MEASURE SUB VIEWGROUP");
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
child.layout(0, 0, getWidth(), getHeight());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
评论表示赞赏.提前致谢.
因为您必须将度量实际传递给子视图:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.i("boxchart","INSIDE ON MEASURE SUPER VIEWGROUP");
final int count = getChildCount();
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
//Make or work out measurements for children here (MeasureSpec.make...)
measureChild (child, widthMeasureSpec, heightMeasureSpec)
}
}
}
Run Code Online (Sandbox Code Playgroud)
否则你永远不会真正衡量你的孩子.由您来决定如何做到这一点.仅仅因为你SuperViewGroup处于线性布局,你就有SuperViewGroup责任测量它的孩子.
| 归档时间: |
|
| 查看次数: |
4530 次 |
| 最近记录: |