【发布时间】:2014-08-03 17:02:57
【问题描述】:
我正在使用一个简单的水平 LinearLayout 将 3 个按钮彼此相邻放置,宽度相同。我通过以下 xml 文本实现了这一点:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:baselineAligned="false" >
<Button
android:id="@+id/help"
style="android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/help" />
<Button
android:id="@+id/close"
style="android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/closemultigame" />
<Button
android:id="@+id/ok"
style="android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/restartmultigame" />
</LinearLayout>
现在布局看起来像这样:
按钮 layout_height 被定义为 wrap_content,但最右边的按钮不包裹其内容。它在不同的设备上看起来不同,在某些设备上看起来不错。 我实际上想要实现的是三个按钮,相同的宽度和相同的高度,每个按钮的文本水平和垂直居中。你会建议什么方法?
添加:整个布局按要求:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:drawableLeft="@drawable/matchandfit"
android:drawablePadding="@dimen/activity_horizontal_margin"
android:gravity="center_vertical"
android:padding="@dimen/activity_horizontal_margin"
android:text="@string/multigameover"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/multigameresult"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:id="@+id/masterresult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/masterresultinfo"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:baselineAligned="false" >
<Button
android:id="@+id/help1"
style="android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/help" />
<Button
android:id="@+id/close1"
style="android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/closemultigame" />
<Button
android:id="@+id/ok1"
style="android:buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/restartmultigame" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
正如 Wasim Ahmed 所指出的,这是由于 ScrollView。现在我使用 ScrollView 来确保按钮即使在小型设备上的横向状态下也可以访问。 (布局用于对话框)。是否有其他方法可以始终保持按钮可访问/可见?
解决方法: 我发现的解决方案或更好的解决方法是将 TextView 添加到封闭的 LinearLayout 的末尾。这个 TextView 被垂直剪裁到其高度的一半左右,但它不包含文本,所以没有人会注意到。在封闭的 LinearLayout 的底部添加一些填充没有帮助
【问题讨论】:
-
我刚刚尝试了您的代码,它在这里工作正常..您的代码没有问题..我认为您的 LInearLayout 在另一个布局内..所以它绑定到它的高度..跨度>
-
是的,你是对的,但外部线性布局也有一个 wrap_content 的高度。
-
你能发布整个布局 Xml 吗? :)
-
是的,在我的帖子中附加了整个布局
-
亲爱的,我检查了它...只是因为滚动视图。