求大神解决:谢谢现象描述:我自定义了一个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
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就不正常显示了。求大神解决,谢谢!困扰了好久,,求解!谢谢...
在OnMeasure()中添加了如下几句代码:
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(widthSpecSize, heightSpecSize);
View child=getChildAt(0);
child.measure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthSpecSize, heightSpecSize);PS:能不能加你好友啊?希望以后多多想你请教!
见5楼,我已经贴出来了,我的原因是在measure的时候没有measure子view。解决方法见5楼,希望可以对你有帮助。