【发布时间】:2017-02-22 22:23:40
【问题描述】:
我在水平LinearLayout 中的ImageButton 旁边有一个TextView,然后将其包裹在垂直LinearLayout 中。为了让按钮显示出来,我必须在文本视图和按钮上设置一个layout_weight,并且按钮是两者中更强的。然而,这似乎使按钮决定了它们的高度,即使文本需要更高。有没有办法解决这个问题?
<LinearLayout
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_width="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="@color/text_color"
android:textSize="@dimen/medium_font"
android:layout_gravity="center"
android:text="A long text that wraps correctly but then gets cut off"
android:layout_weight="1" />
<ImageButton
android:contentDescription="@string/share_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/share_button"
android:padding="5dp"
android:layout_weight="0"
android:layout_gravity="center"
android:scaleType="center"
android:background="@android:color/transparent"
android:src="@mipmap/ic_menu_share_holo_dark"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_background"
android:id="@+id/distances_parent">
<Spinner
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/distance_labels"
android:id="@+id/distances" />
</LinearLayout>
<Space
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
<FrameLayout
android:background="@color/back_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
...
</FrameLayout>
</LinearLayout>
这就是它的样子:
如您所见,按钮决定了高度,而不是文字。
这就是我想要的样子:
【问题讨论】:
-
可以使用RelativeLayout 代替LinearLayout 来实现这一点。请提供您的预期输出为图像[绘图]。
-
我已经用我想要的样子编辑了帖子。
-
你有一个相当复杂的布局。我建议把它拆掉,看看你是否能让核心部分——如屏幕截图所示——正常工作。您也可以在第二个 LinearLayout 中尝试 XML 属性 android:measureWithLargestChild。