自定义ViewGroup中不能正确显示 其它的viewGroup

yuanzi 2011-05-24 11:49:35
问题描述:

小弟写了一个MyViewGroup(extends ViewGroup),在MyViewGroup中可以正确显示child view都是widget类型的(例如Button,ImageButton),但不能正确显示其它的ViewGroup比如系统的RelativeLayout。我在xml中RelativeLayout里面只定义了一个ImageButton ,这个ImageButton能够显示出来但显示的位置与xml中定义的不同。

测试类 testViewClass.java :


package com.yht.testViewClass;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

public class testViewClass extends Activity {
/** Called when the activity is first created. */
MyViewGroup mvg;
LayoutInflater layoutInflater;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


mvg = new MyViewGroup(this);
this.setContentView(mvg);
layoutInflater = this.getLayoutInflater();
RelativeLayout view = (RelativeLayout) layoutInflater.inflate(R.layout.mute_box, null);
mvg.addView(view);

}
}



MyViewGroup.java


package com.yht.testViewClass;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.LinearLayout;

public class MyViewGroup extends ViewGroup {

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

}

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

}
@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());
}
}

}


相对布局的xml文件 mute_box.xml:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="@drawable/list_background">

<!--
Button 相对于Parent 垂直靠右显示
-->
<Button
android:id="@+id/image_button_2"
android:text="TEST BUTTON"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>



</RelativeLayout>


...全文
1193 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyq 2014-02-12
  • 打赏
  • 举报
回复
调用 ViewGroup.addView(View child, ViewGroup.LayoutParams params) 就可以了。后面要添加LayoutParams 参数。( LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);)ViewGroup嵌套其它布局时子view的宽度和高度默认为wrap_content。  mvg.addView(view,new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
俏鹏 2014-01-03
  • 打赏
  • 举报
回复
我在ViewGroup下边放置一个TextView设置文本框中文字的对齐方式不管用···
winson_jason 2012-11-19
  • 打赏
  • 举报
回复
我也遇到这样的问题!
Marco_Lee 2012-10-29
  • 打赏
  • 举报
回复
//心中疑惑:我认为我只要在这里调用child的layout(int,int,int,int)方法给出它的位置和宽高就可以了
//如果child本身是一个ViewGroup的话它是否应该自己去管理它本身内部的child的位置和宽度呢???


同样疑惑。。。求高手解答。。遇到同样问题了~~~
sandshowboy 2011-12-06
  • 打赏
  • 举报
回复
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int count = getChildCount();
for (int i = 0; i < count; i++) {
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
}
}
填上这个就可以了
avi9111 2011-07-12
  • 打赏
  • 举报
回复
我也碰到这个问题啊,只能显示VIEW
changy 2011-05-24
  • 打赏
  • 举报
回复
关注,帮顶

80,471

社区成员

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

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