Button没有包装其内容

时间:2014-06-13 07:45:29

标签: android button layout

我使用一个简单的水平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>

现在布局如下: screenshot from designer

按钮layout_height定义为wrap_content,但最右边的按钮不包含其内容。它在不同的设备上看起来不同,有些看起来不错。 我实际想要实现的是三个按钮,相同的宽度和相同的高度,每个按钮的文本水平和垂直居中。你会提出什么方法?

ADDED:请求的整个布局:

<?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底部添加一些填充没有帮助

5 个答案:

答案 0 :(得分:3)

我一次又一次地发现即使您指定Buttonandroid:layout_height="wrap_content"也不会调整文字大小。

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

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

2。使用TextView代替{在指定android:layout_height="wrap_content"时确实正确包装文字),并在{{1}上设置onClickListener模拟按钮的行为。

答案 1 :(得分:1)

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

答案 2 :(得分: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>

答案 3 :(得分:-1)

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

答案 4 :(得分:-1)

解决此问题的方法是设置minHeight Button这样的<{p}}

android:minHeight="1dp"

应该有效

相关问题