高分求解,自定义ViewGroup里面放一个ViewGroup,无法正常显示

Marco_Lee 2012-10-30 11:08:39
如题,我自定义了一个ViewGroup,onMeasure和onLayout都做了常规处理,然后我在xml里或是动态使用,在里面放置View可以正常显示完全没有问题,

可是里面放一个ViewGroup(如LinearLayout)就不行了。

子ViewGroup的大小和位置没有问题,问题是子ViewGroup里面的子View的大小和位置出现了问题(没有按照我写的布局的方式排列,大小也不受我写的布局控制)!

如下,第一个ImageButton正常,第二个放在LinearLayout的ImageButton就不正常了,对它设置的属性完全无效

<com.example.swipeview.MyViewGroup
android:id="@+id/swap_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_dark"
android:src="@drawable/ic_action_search"
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageButton
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:src="@drawable/ic_launcher"
/>

</LinearLayout>


</com.example.swipeview.MyViewGroup>




百度google了一下发现有类似问题的帖子很多,但是都没有解答。。。

如果我的问题描述不够清楚,大家可以看看这个帖子
http://topic.csdn.net/u/20110524/11/dcde4122-fdb6-4852-92dd-7f35e4dc0c7f.html#top

“心中疑惑:我认为我只要在这里调用child的layout(int,int,int,int)方法给出它的位置和宽高就可以了
如果child本身是一个ViewGroup的话它是否应该自己去管理它本身内部的child的位置和宽度呢???
...全文
438 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
搬不搬砖 2012-10-30
  • 打赏
  • 举报
回复
解决方法1,把child.measure移动到onMeasure中就可以了
解决方法2,不继承viewgroup,而是继承framelayout,因为framelayout会替你做child.measure

要记得把onlayout中的child.measure去掉。

我猜测在onmeasure和onlayout之间还有很多别的东西要执行,如果把child.measure放到onlayout中来做,就会导致在onlayout开始时有些数据没有准备好,而这些数据本该在onlayout开始之前就准备好了。
Marco_Lee 2012-10-30
  • 打赏
  • 举报
回复
非常感谢,虽然我还没理解当中的原理.

[Quote=引用 6 楼 的回复:]

继承viewgroup要重写onMeasure,下面的代码是可以的,

Java code
public class MyViewGroup extends ViewGroup {

public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);

……
[/Quote]
搬不搬砖 2012-10-30
  • 打赏
  • 举报
回复
继承viewgroup要重写onMeasure,下面的代码是可以的,

public class MyViewGroup extends ViewGroup {

public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);

}

public MyViewGroup(Context context) {
super(context);

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int count = this.getChildCount();

for(int i = 0;i < count;i++){
View child = this.getChildAt(i);

child.measure(widthMeasureSpec, heightMeasureSpec);

}
}

@Override
protected void onLayout(boolean arg0, int l, int t, int r, int b) {

int count = this.getChildCount();

for(int i = 0;i < count;i++){
View child = this.getChildAt(i);
child.setVisibility(View.VISIBLE);
//child.measure(r-l, b-t);
int x = l;
int y = t;
//心中疑惑:我认为我只要在这里调用child的layout(int,int,int,int)方法给出它的位置和宽高就可以了
//如果child本身是一个ViewGroup的话它是否应该自己去管理它本身内部的child的位置和宽度呢???
child.layout(x,y,x + getWidth(),y + getHeight());
}
}

}
Marco_Lee 2012-10-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

如果是继承FrameLayout,就不要重写onMeasure了,下面这段代码就可以正确显示
Java code
public class MyViewGroup extends FrameLayout {

public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);……
[/Quote]

对,你是对的。继承FrameLayout不重写onMeasure就正常了。。。

不过为什么重写onMeasure就不行了。。和为什么继承ViewGroup重写onMeasure也不行呢。。。
搬不搬砖 2012-10-30
  • 打赏
  • 举报
回复
如果是继承FrameLayout,就不要重写onMeasure了,下面这段代码就可以正确显示
public class MyViewGroup extends FrameLayout {

public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);

}

public MyViewGroup(Context context) {
super(context);

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

@Override
protected void onLayout(boolean arg0, int l, int t, int r, int b) {

int count = this.getChildCount();

for(int i = 0;i < count;i++){
View child = this.getChildAt(i);
child.setVisibility(View.VISIBLE);
//child.measure(r-l, b-t);
int x = l;
int y = t;
//心中疑惑:我认为我只要在这里调用child的layout(int,int,int,int)方法给出它的位置和宽高就可以了
//如果child本身是一个ViewGroup的话它是否应该自己去管理它本身内部的child的位置和宽度呢???
child.layout(x,y,x + getWidth(),y + getHeight());
}
}

}
搬不搬砖 2012-10-30
  • 打赏
  • 举报
回复
我在你之前那个帖子的基础上修改了,结果正确啊,按钮在屏幕的右边
Marco_Lee 2012-10-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

解决方法1,把child.measure移动到onMeasure中就可以了
解决方法2,不继承viewgroup,而是继承framelayout,因为framelayout会替你做child.measure

要记得把onlayout中的child.measure去掉。

我猜测在onmeasure和onlayout之间还有很多别的东西要执行,如果把child.measure放到onl……
[/Quote]

我就是继承FrameLayout的,我也是在onMeasure中对child执行measure的,onLayout中我并没有对child进行measure,onLayout中我只执行child.layout
但是这样,问题依旧。。。

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧