Android片段不将匹配父项视为高度

Android片段不将匹配父项视为高度,android,center,android-linearlayout,fragment,android-framelayout,Android,Center,Android Linearlayout,Fragment,Android Framelayout,很抱歉,代码被大量转储,但我真的迷路了 MyActivity.java onCreate: super.onCreate(savedInstanceState); setContentView(R.layout.activity_singlepane_empty); mFragment = new PlacesFragment(); getSupportFragmentManager().beginTransaction() .add(R.id.root_

很抱歉,代码被大量转储,但我真的迷路了

MyActivity.java onCreate

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_singlepane_empty);
mFragment = new PlacesFragment();
getSupportFragmentManager().beginTransaction()
                    .add(R.id.root_container, mFragment)
                    .commit();
mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
return mRootView;
<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>
<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>
PlacesFragment.java onCreateView

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_singlepane_empty);
mFragment = new PlacesFragment();
getSupportFragmentManager().beginTransaction()
                    .add(R.id.root_container, mFragment)
                    .commit();
mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
return mRootView;
<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>
<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>
注意事项
mRootView
是一个
视图组
全局视图,我相信这没有问题<代码>位置片段是一个
列表片段

布局:

活动\u单一窗格\u empty.xml

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_singlepane_empty);
mFragment = new PlacesFragment();
getSupportFragmentManager().beginTransaction()
                    .add(R.id.root_container, mFragment)
                    .commit();
mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
return mRootView;
<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>
<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>

列表内容.xml

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_singlepane_empty);
mFragment = new PlacesFragment();
getSupportFragmentManager().beginTransaction()
                    .add(R.id.root_container, mFragment)
                    .commit();
mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
return mRootView;
<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>
<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\u视图时,
FrameLayout
不会填充整个屏幕

root\u容器
为蓝色,
FrameLayout
为黄色,请参见下文以进行调试黄色窗格不应该填满整个屏幕吗


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

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


我也遇到了同样的问题,并且认为当您将片段的onCreateView中的布局膨胀为null时会发生这种情况,就像您在这里所做的那样:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
相反,您必须这样做:

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

其中container是视图组。至少,这解决了我的问题。

我这样做的方式是用xml代码创建一个透明的图像,将片段设置为相对布局,并在
图像视图
中设置
布局\u alignParentBottom=“true”
,这样图像会粘在底部,并使片段填充父对象

代码如下:

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_invests.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" />



我不得不将layout\u height=“wrap\u content”更改为“match\u parent”以使其正常工作。

我也遇到了同样的问题&我尝试了很多方法,但都没有成功

面临什么问题?

我在
内部安装了
,其高度设置为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>


要使其正常工作,请将所有外部包含视图更改为“匹配父视图”

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


android:fillViewport=“true”

可选视图,作为生成的层次结构的父级(如果attachToRoot为true),或者只是一个为返回的层次结构的根提供一组LayoutParams值的对象(如果attachToRoot为false)。接受回答。谢谢。这也是一个古老的答案。从那以后,我把录取分数改为诺曼的答案。当然,这个答案提供了更多的灵活性,但我很高兴接受他的回答,因为这个问题显然保证了一个易于使用的解决方案。如果您使用ScrollView签出此选项: