Android Fragment 不尊重 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中的设计预览上,是可以的。仅当作为片段添加到 root_view
时,FrameLayout
才不会填充整个屏幕。
root_container
为蓝色,FrameLayout
为黄色,请参阅下文以进行调试。 黄色窗格不应该填满整个屏幕吗?!?!?!?
Sorry for the huge code dump, but I'm truly lost.
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;
Notes: mRootView
is a ViewGroup
global, no problem about it, I believe. PlacesFragment
is a ListFragment
.
Layouts:
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>
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.
root_container
is blue, and FrameLayout
is yellowish, see below for debug purposes. Shouldn't the yellow pane fill the whole screen?!?!?!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我遇到了同样的问题,并且认为当您用 null 膨胀 Fragment 的 onCreateView 中的布局时就会发生这种情况,就像您在这里所做的那样:
相反,您必须这样做:
其中容器是 Viewgroup。至少,这为我解决了问题。
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:
Instead you have to do this:
Where container is the Viewgroup. At least, that solved the problem for me.
由于某种原因,FrameLayout 无法从 XML 获取其布局。
我还需要在代码中设置它(Fragment 的 onCreateView):
For some reason the FrameLayout does not get its layout from the XML.
I need to set it also in code (Fragment's onCreateView):
我必须将layout_height =“wrap_content”更改为“match_parent”才能使其工作。
I had to change layout_height="wrap_content" to "match_parent"to make it work.
我这样做的方法是在 xml 代码中创建一个透明图像,使片段成为相对布局,并在
ImageView
中创建layout_alignParentBottom="true"
,这样图像粘在底部并使片段填充父级:
temp_transparent.xmlfragment_my_invites.xml
代码
The way I did that was to create a transparent image in xml code, make the fragment a relative layout and make
layout_alignParentBottom="true"
in theImageView
, this way the image sticks to the bottom and makes the fragment fill the parentCode Here:
temp_transparent.xml
fragment_my_invites.xml
我有同样的问题&我尝试了很多方法,但没有一个起作用。
遇到什么问题?
我在
内设置了
,其高度设置为 0dp,这导致了问题。为了使其正常工作,将所有外部包含视图更改为 match_parent
I had the same problem & i tried many things but none of them worked.
What problem faced?
I had
<FrameLayout>
inside<ScrollView>
which height is set to 0dp which causes the problem.To make it working change all the outer containing views to match_parent
将这一行添加到父布局将解决该问题,
android:fillViewport="true"
Adding this line to the Parent Layout will solve that problem,
android:fillViewport="true"