Sorry for the huge code dump, but I'm truly lost.
抱歉,巨大的代码转储,但我真的迷路了。
MyActivity.java onCreate:
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:
PlacesFragment.java onCreateView:
mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
return mRootView;
Notes: mRootView is a ViewGroup global, no problem about it, I believe. PlacesFragment is a ListFragment.
注意:mRootView是一个ViewGroup全局,我相信没问题。 PlacesFragment是一个ListFragment。
Layouts:
布局:
activity_singlepane_empty.xml:
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:
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>
Problem: as you can see, the expected behavior would be to have the empty TextView above to appear centered on the screen. On the design preview in Eclipse, it is OK. Only when added to root_view as a fragment the FrameLayout won't fill the whole screen.
问题:正如您所看到的,预期的行为是让上面的空TextView在屏幕上居中显示。在Eclipse的设计预览中,没关系。只有当作为片段添加到root_view时,FrameLayout才会填满整个屏幕。
root_container is blue, and FrameLayout is yellowish, see below for debug purposes. Shouldn't the yellow pane fill the whole screen?!?!?!?
root_container为蓝色,FrameLayout为黄色,请参阅下面的调试目的。黄色窗格不应该填满整个屏幕吗?!?!?!?
4 个解决方案
#1
69
I had the same problem and think it happens when you inflate the layout in the Fragment's onCreateView with null, like you did here:
我遇到了同样的问题,并认为当你在片段的onCreateView中使用null填充布局时就会发生这种情况,就像你在这里做的那样:
mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
Instead you have to do this:
相反,你必须这样做:
mRootView = (ViewGroup) inflater.inflate(R.layout.list_content,container, false);
Where container is the Viewgroup. At least, that solved the problem for me.
容器是Viewgroup的位置。至少,这解决了我的问题。
#2
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>
I had to change layout_height="wrap_content" to "match_parent"to make it work.
我不得不将layout_height =“wrap_content”更改为“match_parent”以使其正常工作。
#3
3
For some reason the FrameLayout does not get its layout from the XML.
由于某种原因,FrameLayout没有从XML获取其布局。
I need to set it also in code (Fragment's onCreateView):
我需要在代码中设置它(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;
#4
0
The way I did that was to create a transparent image in xml code, make the fragment a relative layout and makelayout_alignParentBottom="true"
in the ImageView
, this way the image sticks to the bottom and makes the fragment fill the parent
我这样做的方法是在xml代码中创建透明图像,在ImageView中使片段成为相对布局并使makelayout_alignParentBottom =“true”,这样图像就会粘到底部并使片段填充父片段
Code Here:
代码在这里:
temp_transparent.xml
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
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" />
#1
69
I had the same problem and think it happens when you inflate the layout in the Fragment's onCreateView with null, like you did here:
我遇到了同样的问题,并认为当你在片段的onCreateView中使用null填充布局时就会发生这种情况,就像你在这里做的那样:
mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
Instead you have to do this:
相反,你必须这样做:
mRootView = (ViewGroup) inflater.inflate(R.layout.list_content,container, false);
Where container is the Viewgroup. At least, that solved the problem for me.
容器是Viewgroup的位置。至少,这解决了我的问题。
#2
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>
I had to change layout_height="wrap_content" to "match_parent"to make it work.
我不得不将layout_height =“wrap_content”更改为“match_parent”以使其正常工作。
#3
3
For some reason the FrameLayout does not get its layout from the XML.
由于某种原因,FrameLayout没有从XML获取其布局。
I need to set it also in code (Fragment's onCreateView):
我需要在代码中设置它(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;
#4
0
The way I did that was to create a transparent image in xml code, make the fragment a relative layout and makelayout_alignParentBottom="true"
in the ImageView
, this way the image sticks to the bottom and makes the fragment fill the parent
我这样做的方法是在xml代码中创建透明图像,在ImageView中使片段成为相对布局并使makelayout_alignParentBottom =“true”,这样图像就会粘到底部并使片段填充父片段
Code Here:
代码在这里:
temp_transparent.xml
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
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" />