【问题标题】:Button is not wrapping its content按钮未包装其内容
【发布时间】: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 吗? :)
  • 是的,在我的帖子中附加了整个布局
  • 亲爱的,我检查了它...只是因为滚动视图。

标签: android button layout


【解决方案1】:

我一次又一次地观察到Button不会调整到文本大小,即使您指定了android:layout_height="wrap_content"

您可以做以下两件事之一:

1.您可以手动设置Button 的文字大小或宽度,直到文字适合(模拟wrap_content 的外观)。

2. 改用TextView(当您指定android:layout_height="wrap_content" 时,它会正确换行文本)并在TextView 上设置onClickListener 来模拟按钮。

【讨论】:

  • TextView 有同样的问题。正如 Wasim Ahmed 所指出的,这是由于周围的 ScrollView...
【解决方案2】:

不要包裹按钮的高度,而是使用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>

结果:

【讨论】:

  • 复制了您的 xml 文本,但在我的系统上仍然有相同的效果(Android 4.2. Eclipse 中的布局渲染器,但在我的 Google nexus 7 上的结果相同)
  • @Gerhard 但它是大多数设备吗??
  • 这是我设备的 50% :)
  • @Gerhard 更改填充以匹配父级并用结果更新我
  • 填充父级和匹配父级是一样的,不是吗?两者都评估为“-1”
【解决方案3】:

解决方法是像这样设置minHeight of Button

android:minHeight="1dp"

它应该可以工作

【讨论】:

    【解决方案4】:

    我刚试过,效果很好:

    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>
    

    【讨论】:

      【解决方案5】:

      没有运气使用 android:layout_width="wrap_content" 等在切换按钮中包装文本,所以我在我想要换行的标签字符串中添加了反斜杠 n (\n)。 例如, “按开始” 变成了, "按\n到\n开始" 这对我有用。

      【讨论】:

        【解决方案6】:

        尝试使用 android:layout_width="wrap_content" 而不是 android:layout_width="0dp" 并查看 id

        【讨论】:

        • layout_width="wrap_content" 使按钮具有不同的宽度
        • 然后尝试手动设置调整android:layout_height,使所有按钮高度一致,不截断文字!!
        • 如果我手动设置layout_height,它可以工作,但是不同的设备呢?
        • widht 必须是 match_Parent..在这种情况下
        • 尝试使用 sp 而不是 dp 来调整字体大小。这应该根据设备而变化。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-11
        • 1970-01-01
        相关资源
        最近更新 更多