Android 如何做';在按钮文本中添加新行并增加按钮大小时,是否更改按钮的大小?

Android 如何做';在按钮文本中添加新行并增加按钮大小时,是否更改按钮的大小?,android,xml,button,Android,Xml,Button,例如,我有一个垂直线性布局,有两个按钮,里面有android:layout\u weight=“1”。问题是,如果我使用 并增大其大小,该按钮将比另一个按钮大。如果文本大小已更改,如何使按钮不更改其大小 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&

例如,我有一个垂直线性布局,有两个按钮,里面有
android:layout\u 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>

在按钮代码中替换

        android:layout_height="wrap_content"


它将起作用:)

在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>


已尝试在您的
按钮中使用
android:maxLines=“1”
android:ellipsize=“end”
,谢谢您的回复。它没有按我想要的那样工作。如果文本是“myname”,现在显示的是我的…你能提供XML代码吗?@PhilipDaniel请在下面检查我的答案,伙计@PhilipDaniel能否请您将您的预期输出与问题分享为图像或屏幕快照?我以前试过这样做。它使按钮变小而不是变大。我希望它的大小与上面的一样。是的,我应该是按钮的范围,它只会使两个按钮占据一半的空间,没有更多,没有更多less@MohanKumar谢谢,一开始我没有意识到我必须将0dp设置为上下按钮。
<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>