首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

ViewGroup中用LayoutInflater显示不出来!求解!多谢

2012-03-14 
ViewGroup中用LayoutInflater显示不出来!求解!谢谢。求大神解决:谢谢现象描述:我自定义了一个View继承ViewG

ViewGroup中用LayoutInflater显示不出来!求解!谢谢。
求大神解决:谢谢

现象描述:我自定义了一个View继承ViewGroup,ViewGroup添加button,textView什么的都可以显示。但是如果是我用LayouInflater 的对象从一个xml布局文件中实例化一个View。然后addView()。就是显示不出来。注:如果xml中设置了背景那么可以显示背景色,但是xml中的button等都显示不出来。
代码如下:
  自定义View
  public class TestView extends ViewGroup {
   
  private LayoutInflater mInflater = null;
   
  private View mView;
   
  private TextView mText = null;
   
  private int mWidth, mHeight;
   
  public TestView(Context context) {
  this(context, null, 0);
  }
   
  public TestView(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
  }

  public TestView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  // TODO Auto-generated constructor stub
  mInflater = LayoutInflater.from(context);
  mView = mInflater.inflate(R.layout.title, null);
  addView(mView);
   
  mText = new TextView(context);
  mText.setText("测试LayoutInflater");
  addView(mText);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  // TODO Auto-generated method stub
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  mWidth = getMeasuredWidth();
  mHeight = getMeasuredHeight();
  }
   
  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
  // TODO Auto-generated method stub

  int left, top, right, bottom;
  left = 0;
  top = 0;
  right = mWidth;
  bottom = mHeight / 8;
  mView.layout(left, top, right, bottom);
   
  left = mWidth / 4;
  top = mHeight / 4;
  right = left + mWidth / 3;
  bottom = top + mHeight / 8;
  mText.layout(left, top, right, bottom);
  }

}


title.xml的布局文件:
  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/title_test"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#FFFF0000"
  >
  
  <TextView 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="介绍"
  android:textSize="20sp"/>
   
   
  <Button 
  android:id="@+id/title_back"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:text="返回"
  />
   
</RelativeLayout>

最后在Activity中setContentView(new TestView(this))

显示结果是:
  通过LayoutInfalter实例化的View(即:xml中配置的Relative)只显示了一个背景色,没有显示button和text。
  而添加的textview可以显示。
求解答,,,

PS:如果直接在一个Activity中运用LayoutInflater从一个xml文件中实例化一个View。然后setContentView()这个View,显示是没有问题的。就是只要将其放入一个ViewGroup中,再setContentView就不正常显示了。

求大神解决,谢谢!困扰了好久,,求解!谢谢...

------解决方案--------------------


还是要贴一个解决办法的,以后有人搜到帖子了。个人感觉在自定义view中插入xml布局有点不方便,主要是目前在网上看见的解决办法是讲inflate出来的view的子view也进行测量分配空间,完成layout()函数。这样就比较麻烦了。不知道楼主怎么解决的。
[解决办法]
为什么不讲你自定义的View加到title.xml布局文件中呢?然后在Activity中setContentView(R.layout.title)

热点排行