在按钮文本中添加新行并增加按钮大小时,如何不更改按钮的大小?

时间:2019-09-27 06:07:17

标签: android xml button

例如,我有一个垂直线性布局,带有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>

2 个答案:

答案 0 :(得分:0)

在您的按钮代码中 REPLACE

        android:layout_height="wrap_content"

使用

        android:layout_height="0dp"

它将起作用:)

答案 1 :(得分:0)

将其替换为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>