【问题标题】:How to don't change a size of the button when adding a new line to the button text and increasing the button size?向按钮文本添加新行并增加按钮大小时如何不更改按钮的大小?
【发布时间】:2020-09-27 15:25:44
【问题描述】:

例如,我有一个带有 2 个按钮的垂直线性布局,里面有 android:layout_weight="1"。问题是,如果我使用
 在按钮文本中添加新行并增加其大小,则该按钮会变得比另一个大。如果更改了文本大小,如何使按钮不更改其大小?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ACC1"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <Button
            android:id="@+id/button18"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/colorofbuttons"
            android:fontFamily="@font/architects_daughter"
            android:text="Mom"
            android:textAllCaps="false"
            android:textColor="#ffffff"
            android:textSize="18sp"></Button>


        <Button
            android:id="@+id/button19"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/colorofbuttons"
            android:ellipsize="end"
            android:fontFamily="@font/architects_daughter"
            android:maxLines="1"
            android:text="My&#10;Name"
            android:textAllCaps="false"
            android:textColor="#ffffff"
            android:textSize="18sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <Button
            android:id="@+id/button20"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/colorofbuttons"
            android:fontFamily="@font/architects_daughter"
            android:text="Dad"
            android:textAllCaps="false"
            android:textColor="#ffffff"
            android:textSize="18sp" />

        <Button
            android:id="@+id/button21"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/colorofbuttons"
            android:fontFamily="@font/architects_daughter"
            android:text="Girl"
            android:textAllCaps="false"
            android:textColor="#ffffff"
            android:textSize="18sp" />
    </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 已尝试在您的button 中使用android:maxLines="1"android:ellipsize="end"
  • 感谢您的回复。它没有按我的意愿工作。如果文本是“我的name”,现在显示我的...
  • 能否提供XML代码?
  • @PhilipDaniel 在下面查看我的答案!
  • @PhilipDaniel 您能否将您的预期输出与问题分享为图像或屏幕截图

标签: android xml button


【解决方案1】:

在您的按钮代码中REPLACE

        android:layout_height="wrap_content"

        android:layout_height="0dp"

它会起作用的:)

【讨论】:

  • 我以前试过这个。它使按钮变小而不是变大。我希望它和上面那个一样大。
  • 是的,我应该是按钮的范围,它只会让两个按钮占据一半一半的空间,不多也不少
  • @MohanKumar 谢谢你,一开始我没有意识到我必须将 0dp 设置为上下按钮。
【解决方案2】:

在你的 XML 中替换它

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ACC1"
    android:weightSum="2"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:weightSum="1"
        android:orientation="vertical">

        <Button

            android:id="@+id/button18"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight=".5"
            android:text="Mom"
            android:textAllCaps="false"
            android:textColor="#ffffff"
            android:textSize="18sp"/>


        <Button
            android:id="@+id/button19"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight=".5"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="My&#10;Name"
            android:textAllCaps="false"
            android:textColor="#ffffff"
            android:textSize="18sp" />
    </LinearLayout>

    <LinearLayout
       android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:weightSum="1"
        android:orientation="vertical">


        <Button
            android:id="@+id/button20"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight=".5"
            android:text="Dad"
            android:textAllCaps="false"
            android:textColor="#ffffff"
            android:textSize="18sp" />

        <Button
            android:id="@+id/button21"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:layout_weight=".5"
            android:text="Girl"
            android:textAllCaps="false"
            android:textColor="#ffffff"
            android:textSize="18sp" />
    </LinearLayout>
</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    相关资源
    最近更新 更多