50

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

MyActivity.java onCreate

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

PlacesFragment.java onCreateView

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
return mRootView;

注释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>

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>

问题:如您所见,预期的行为是让TextView上面的空白显示在屏幕中央。在 Eclipse 中的设计预览上,是可以的。只有当root_view作为片段添加时,FrameLayout才会填满整个屏幕。

root_container是蓝色的,并且FrameLayout是淡黄色的,请参见下面的调试目的。黄色窗格不应该填满整个屏幕吗?!?!?!?

在此处输入图像描述

4

6 回答 6

76

我遇到了同样的问题,并认为当您使用 null 为 Fragment 的 onCreateView 中的布局充气时会发生这种情况,就像您在此处所做的那样:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);

相反,您必须这样做:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content,container, false);

其中容器是视图组。至少,这为我解决了问题。

于 2012-03-12T14:26:38.790 回答
6

由于某种原因,FrameLayout 没有从 XML 中获取其布局。

我还需要在代码中设置它(片段的 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;
于 2011-10-28T22:54:18.277 回答
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>

我必须将 layout_height="wrap_content" 更改为 "match_parent" 才能使其正常工作。

于 2016-05-06T14:09:24.810 回答
0

我这样做的方法是在 xml 代码中创建一个透明图像,使片段成为相对布局并layout_alignParentBottom="true"在 中制作ImageView,这样图像会粘在底部并使片段填充父级

代码在这里:

temp_transparent.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:thickness="0dp"
   android:shape="rectangle">
<gradient
    android:startColor="#00000000"
    android:endColor="#00000000"
    android:type="linear"
    android:angle="270"/>

fragment_my_invites.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:id="@+id/my_invites_fragment"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="#ffffff">


<ListView
    android:id="@+id/list_invites"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:layout_alignParentTop="true" >
</ListView>

<ImageView
    android:id="@+id/transparent_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/list_invites"
    android:src="@drawable/temp_transparent" />

于 2015-05-26T21:42:15.330 回答
0

我遇到了同样的问题,我尝试了很多东西,但都没有奏效。

面临什么问题?

我在<FrameLayout>里面<ScrollView>的高度设置为 0dp 这会导致问题。

<ScrollView
   android:id="@+id/scrollView2"
   android:layout_width="0dp"
   android:layout_height="0dp"
   android:background="@color/white"
   app:layout_constraintBottom_toTopOf="@+id/bottom_nav_view"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintTop_toBottomOf="@+id/myToolbar">

      <FrameLayout
        android:id="@+id/mainFrame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/myToolbar" />
</ScrollView>

为了使其正常工作,将所有外部包含的视图更改为match_parent

于 2020-01-14T13:40:18.043 回答
0

将此行添加到父布局将解决该问题,

android:fillViewport="true"

于 2021-05-11T11:37:55.450 回答