【问题标题】:Buttons with multiline text sink under alignment line, how to fix it?在对齐线下带有多行文本的按钮,如何解决?
【发布时间】:2012-02-26 02:33:39
【问题描述】:

我看到简单按钮的奇怪行为,当它们的文本分成几行时。我有它在更复杂的情况下,但即使在最简单的情况下也会发生。使用这种布局:

<?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="match_parent">

<Button android:layout_width="100dip" android:layout_height="70dip" android:text="111" />
<Button android:layout_width="100dip" android:layout_height="70dip" android:text="111 222 333" />
<Button android:layout_width="100dip" android:layout_height="70dip" android:text="111 222 333 444 555 666" />
<Button android:layout_width="100dip" android:layout_height="70dip" android:text="111" />
</LinearLayout>

结果是:

每下一行文本,按钮都会上下移动。我要他们排成一行。这是一个 android 错误吗?如何修复它?

【问题讨论】:

  • 这不是错误,按钮的文本基线与其他按钮对齐。将baselineAligned 设置为false 或LinearLayout 将解决此问题。

标签: android layout button alignment multiline


【解决方案1】:

像这样代替按钮使用

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="111" />

【讨论】:

  • 我希望这些按钮大小相同。实际上我在我的真实程序中使用了 android:layout_weight="1" 和 android:layout_width="0dip" ,但这并没有帮助。
【解决方案2】:

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="100dip"
            android:layout_height="70dip"
            android:layout_gravity="center"
            android:text="111" />

        <Button
            android:layout_width="100dip"
            android:layout_height="70dip"
            android:layout_gravity="center"
            android:text="111 222 333" />

        <Button
            android:layout_width="100dip"
            android:layout_height="70dip"
            android:layout_gravity="center"
            android:text="111 222 333 444 555 666" />

        <Button
            android:layout_width="100dip"
            android:layout_height="70dip"
            android:layout_gravity="center"
            android:text="111" />
    </LinearLayout>

</LinearLayout>

【讨论】:

  • +1。谢谢!实际上,按钮的 android:layout_gravity="center" 似乎就足够了,不需要嵌套活动。我会再等几个小时等待其他意见,但您的回答可能会得到分数。
  • android:gravity="center" 作为布局属性也可以工作,还有一些其他条件,例如:
【解决方案3】:

在每个按钮中使用以下 2 行,然后所有按钮将出现在单行中。

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

如果您希望这些按钮必须拉伸整个宽度,请将下面的行也添加到每个按钮

    android:layout_weight="1" 

【讨论】:

  • 我知道“wrap_content”的作用,但我想要大小相同的按钮。
【解决方案4】:

做这样的事情

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/btn1"
    android:layout_width="100dip"
    android:layout_height="70dip"
    android:text="111" />

<Button
    android:layout_toRightOf="@+id/btn1"
    android:id="@+id/btn2"
    android:layout_width="100dip"
    android:layout_height="70dip"
    android:text="111 222 333" />

<Button
    android:layout_toRightOf="@+id/btn2"
    android:id="@+id/btn3"
    android:layout_width="100dip"
    android:layout_height="70dip"
    android:text="111 222 333 444 555 666" />

<Button
           android:layout_toRightOf="@+id/btn3"
    android:id="@+id/btn4"
    android:layout_width="100dip"
    android:layout_height="70dip"
    android:text="111" />

</RelativeLayout>

【讨论】:

  • +1。这可行,但来自 dmytrodanylyk 解决方案的 android:layout_gravity="center" 更简单。
【解决方案5】:

设置LinearLayout的android:baselineAligned为false

<?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="match_parent"
android:baselineAligned="false">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 2015-07-06
    • 2014-12-17
    • 2018-05-13
    • 2015-07-04
    • 2018-12-11
    • 1970-01-01
    相关资源
    最近更新 更多