Android Fragment不会将match_parent视为高度

dav*_*ino 43 android center fragment android-linearlayout android-framelayout

对不起,巨大的代码转储,但我真的迷路了.

MyActivity.java onCreate:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_singlepane_empty);
mFragment = new PlacesFragment();
getSupportFragmentManager().beginTransaction()
                    .add(R.id.root_container, mFragment)
                    .commit();
Run Code Online (Sandbox Code Playgroud)

PlacesFragment.java onCreateView:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
return mRootView;
Run Code Online (Sandbox Code Playgroud)

注意:mRootView是一个ViewGroup全局,我相信没问题.PlacesFragment是一个ListFragment.

布局:

activity_singlepane_empty.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_container"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00f">
    <include layout="@layout/actionbar"/>

    <!-- FRAGMENTS COME HERE! See match_parent above -->

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

list_content.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listContainer"
        android:background="#990"
        >

        <ListView android:id="@android:id/list"
                android:layout_width="match_parent" 
                android:layout_height="match_parent"
                android:drawSelectorOnTop="false" />

        <TextView android:id="@id/android:empty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceMedium" 
                android:text="@string/no_data_suggest_connection"/>

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

问题:正如您所看到的,预期的行为是将上面的空TextView显示在屏幕中心.在Eclipse的设计预览中,没关系.只有当作为片段添加到root_view时,FrameLayout才会填满整个屏幕.

root_container为蓝色,FrameLayout为黄色,请参阅下面的调试目的.黄色窗格不应该填满整个屏幕吗?!?!?!?

在此输入图像描述

Nor*_*man 74

我有同样的问题,并认为当你在片段的onCreateView中使用null填充布局时,就像你在这里做的那样:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
Run Code Online (Sandbox Code Playgroud)

相反,你必须这样做:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content,container, false);
Run Code Online (Sandbox Code Playgroud)

容器是Viewgroup的位置.至少,这解决了我的问题.

  • http://developer.android.com/reference/android/view/LayoutInflater.html可选视图是生成的层次结构的父级(如果attachToRoot为true),或者只是为root提供一组LayoutParams值的对象返回的层次结构(如果attachToRoot为false).接受答案.谢谢. (2认同)
  • 另请参见http://stackoverflow.com/questions/5026926/making-sense-of-layoutinflater (2认同)

dav*_*ino 6

由于某种原因,FrameLayout 无法从 XML 获取其布局。

我还需要在代码中设置它(Fragment 的 onCreateView):

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
FrameLayout fl = (FrameLayout) mRootView.findViewById(R.id.listContainer);
fl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return mRootView;
Run Code Online (Sandbox Code Playgroud)


小智 5

<android.support.v4.widget.NestedScrollView
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:fillViewport="true"
    >

    <include layout="@layout/screen_main_fragment" />

</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

我不得不将layout_height ="wrap_content"更改为"match_parent"以使其正常工作.