【发布时间】:2014-09-26 12:33:36
【问题描述】:
我正在尝试使用以下显示逻辑在 Android 中实现布局:
[ Left ][ Right ]
[ Bottom ]
Left 有时比Bottom 宽,有时更窄。当Bottom 比Left 宽时,我希望Right 一直延伸到Bottom 的右边缘;当Left 更宽时,我希望Right 的宽度为0px。
Left 和Bottom 的性质并不那么重要;它们都是 wrap_content 的。
我尝试过在顶层使用 LinearLayout 和 RelativeLayout。无论我做什么,要么最外层容器的宽度由Bottom 驱动,与Left 无关,要么视图扩展不合理,要么Right 始终为零宽度。
这是我对 RelativeLayout 的最新尝试:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="78dp"
>
<TextView
android:id="@+id/Left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left"/>
<View
android:id="@+id/Right"
android:layout_width="0dp"
android:layout_toRightOf="@+id/Left"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_height="1px"
android:layout_marginTop="32dp"
android:background="#ffffffff"/>
<TextView
android:id="@+id/Bottom"
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_below="@+id/Left"
android:layout_alignParentLeft="true"
android:textColor="#ffff00"
android:text="Hello"
android:lines="1"/>
</RelativeLayout>
它没有按预期工作; Right 视图被不合理地拉伸。
在 4.4.2 模拟器上测试。如果重要的话,整个事情都在 ScrollView 中。目标 SDK 为 17。
【问题讨论】:
-
我宁愿使用 LinearLayout 及其 layout_weigth 属性而不是 RelativeLayout
-
你是否将宽度设置为 0,并尝试权重?
-
是的,除此之外。