【发布时间】:2014-09-09 09:50:08
【问题描述】:
背景
我在 FrameLayout/RelativeLayout 中有一个 RelativeLayout(对我来说没关系),它应该在屏幕的底部(就像一个工具栏),并且应该在其中包含一些视图。
它的高度设置为“wrap_content”,它的子视图也是如此。
此布局的子视图是:左侧是 textView,右侧是带有几个按钮的 Horizontal LinearLayout。
问题
似乎无论我做什么,textview 都会导致 RelativeLayout 占据整个空间,而不仅仅是它的孩子。
代码
这是导致此问题的最小 XML 内容。我已经删除了额外的东西(LinearLayout 及其子项,以及一些无关紧要的属性),因为如果我删除 TextView,它们不会导致这个问题:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF000000" >
<!-- Here I've put some views that don't have any relation with the views below, so it doesn't have anything with do with the problem -->
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/vertical_gradient_transparent_to_black" >
<!-- Here I've put a LinearLayout that doesn't cause the problem, so I've removed it for simplicity-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</RelativeLayout>
我尝试了许多可能的属性,还尝试添加其他布局以尝试“欺骗”RelativeLayout,但都没有成功。
问题
为什么会发生?
一个可行的解决方案是使用 Horizontal LinearLayout (带有 TextView 的权重)而不是 RelativeLayout ,但我仍然想知道为什么我不能使用 RelativeLayout 以及它发生的原因。还有如何在仍然使用 RelativeLayout 的同时修复它。
【问题讨论】:
-
可能是因为
android:layout_alignParentBottom="true" -
如果你从
TextView中删除android:layout_alignParentBottom="true"/>它应该可以工作.. -
@user3231871 但是如果我删除它,textView 将位于上部区域,而我希望它位于底部。
标签: android android-layout alignment textview android-relativelayout