Android的片段不尊重match_parent作为高度

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

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的devise预览中,它是可以的。 只有在作为片段添加到root_view时,FrameLayout才会填满整个屏幕。

root_container是蓝色的,FrameLayout是淡黄色的,请参阅下面的debugging目的。 黄色窗格不应该填满整个屏幕?!?!?!?

在这里输入图像说明

我有同样的问题,并认为这发生在你膨胀Fragment的onCreateView布局null,就像你在这里:

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

相反,你必须这样做:

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

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

出于某种原因,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; 
 <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”,使其工作。

我这样做的方式是在xml代码中创build一个透明的图像,使片段相对布局,并在ImageView中使layout_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_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" />