按钮未自适应其内容

12

我正在使用一个简单的水平LinearLayout来使三个按钮并排放置,它们的宽度都相同。我通过以下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来确保即使在小设备的横向模式下,按钮也始终可访问。(布局用于对话框)。是否有其他方法始终保持按钮可访问/可见?

解决方法: 我找到的解决方案或更好的解决方法是在封闭LinearLayout的末尾添加一个TextView。这个TextView在其高度的一半左右被垂直裁剪,但它不包含任何文本,因此没有人会注意到它。将一些填充添加到封闭LinearLayout的底部没有帮助。


我刚刚尝试了你的代码,它在这里运行得很好...你的代码没有任何问题...我认为你的LinearLayout位于另一个布局中...所以它绑定了它的高度.. - Wasim Ahmed
是的,你说得对,但外部线性布局的高度也有一个wrap_content。 - Gerhard
你能发布整个布局XML吗? :) - Wasim Ahmed
是的,我在我的帖子中附加了整个布局。 - Gerhard
1
亲爱的,我检查了一下...这只是由于滚动视图。 - Wasim Ahmed
那么,无法使用scrollview吗?我正在使用scrollview,因为在小设备上横向方向上,对话框可能会变得太高,没有足够的空间放置按钮,导致按钮无法访问。 - Gerhard
6个回答

7
一次又一次地,我发现即使指定了“android:layout_height =”wrap_content“”,Button仍然不会根据文本大小进行调整。
你可以选择以下两种方法之一: 1. 手动设置Button的文本大小或宽度,直到文本适当地适应(模拟wrap_content的外观)。 2. 使用TextView代替Button(当指定“android:layout_height =”wrap_content“”时,文本将正确包装),并在TextView上设置onClickListener以模拟按钮行为。

TextView也有同样的问题。正如Wasim Ahmed所指出的那样,这是由于周围的ScrollView引起的... - Gerhard

7

按钮默认具有minHeight。要删除ButtonminHeight,请执行以下操作:

android:minHeight="0dp"

3
不要使用包裹按钮的高度,而是使用fill_parent使按钮的高度相同。
  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:orientation="horizontal"
    android:baselineAligned="false" >

    <Button
        android:id="@+id/help"
        style="android:buttonBarButtonStyle"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="hilfe" />

    <Button
        android:id="@+id/close"
        style="android:buttonBarButtonStyle"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="beenden" />

    <Button
        android:id="@+id/ok"
        style="android:buttonBarButtonStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="neue Pundo" />

</LinearLayout>

结果:

enter image description here

(无需翻译)

复制了你的 XML 文本,但在我的系统上效果仍然一样(在 Eclipse 中的 Android 4.2 布局渲染器上,在我的 Google Nexus 7 上也是同样的结果)。 - Gerhard
@Gerhard 但这是大多数设备吗?? - Rod_Algonquin
这是我的设备的50% :) - Gerhard
@Gerhard,请将填充方式更改为与父级匹配,并将结果更新给我。 - Rod_Algonquin
填充父级和匹配父级是相同的,不是吗?两者都评估为“-1”。 - Gerhard

0

我刚试了一下,它可以工作:

1)在../res/values/strings.xml中定义:

Line1Line1\nLine2Line2

2)在布局文件中引用它:

<Button
    android:id="@+id/btn_multilines"
    android:text="@string/multilines"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">
</Button>

0

我在使用android:layout_width="wrap_content"等将文本包裹在切换按钮中时,没有成功,所以我在标签字符串中添加了反斜杠n (\n),将其放置在我想要换行的位置。 例如, “按下开始” 变成了, “按下\n开始” 这对我起作用。


-1
尝试使用android:layout_width="wrap_content"而不是android:layout_width="0dp",看看效果如何。

layout_width="wrap_content" 使按钮具有不同的宽度。 - Gerhard
然后尝试手动设置android:layout_height以使所有按钮具有相同的高度且没有文字被截断!! - Elvis Whizzkid
如果我手动设置布局高度,它能够工作,但对于不同的设备怎么办? - Gerhard
宽度必须匹配父级容器..在这种情况下 - Wasim Ahmed
尝试使用sp而不是dp来调整字体大小。这样应该会根据设备进行更改。 - Elvis Whizzkid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接