开发者社区> 问答> 正文

自定义viewGroup中放入fragment view,但是fragment中的内容不显示

自定义viewGroup中放入fragment view,但是fragment中的内容显示不到界面上,明明fragment中的oncreateview都走了,而且返回的view不为空?

<?xml version="1.0" encoding="utf-8"?>
 android:id="@+id/test_viewgroup"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 >
<TextView
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/black"
    />
<!--<TextView
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="sssssss"
    android:background="@android:color/white"
    />-->
<FrameLayout
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</FrameLayout>



protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.test_view_group);
 //mBottomView = (TextView) findViewById(R.id.bottom);
 //mViewGroup = (TestViewGroup) findViewById(R.id.test_viewgroup);
 mTopView.setOnClickListener(this);
 mBottomFragment = new BottomFragment();
 FragmentTransaction transaction = getFragmentManager().beginTransaction();
 transaction.replace(R.id.bottom, mBottomFragment);
 transaction.commit();
 }
    public class BottomFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.test_sliding, container, false);
    return view;
}


}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 Log.i("edward", "onMeasure");
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 // 测量子view的大小
 //measureChildren(widthMeasureSpec, heightMeasureSpec);
 }
@Override
protected void onLayout(boolean changed, int left, int top, int bottom, int right) {
    Log.i("edward", "onLayout");
    int width = getMeasuredWidth();
    int height = getMeasuredHeight();
    for(int i = 0; i < 2; i++) {
        View child = getChildAt(i);
        if (i == 0) {
            child.layout(0, 0, width, height / 2 - mOffset);
        }
        if (i == 1) {
            child.layout(0, height / 2 - mOffset, width, height);
        }
    }
}

fragment 布局中的东西显示不出来

展开
收起
爵霸 2016-06-01 09:28:42 4994 0
1 条回答
写回答
取消 提交回答
  • 自定义viewGroup中onmeasure中必须加上child.measure(widthSpec, heightSpec);

    2019-07-17 19:22:43
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载