【问题标题】:Adjust button text size with button height使用按钮高度调整按钮文本大小
【发布时间】:2017-06-08 05:12:58
【问题描述】:

我有一个按钮,我为其设置了特定的高度,并让按钮文本填满整个按钮。我之前通过将textSize 设置为特定尺寸以填充按钮来实现这一点。

但问题是每当用户更改他/她的设备的系统字体时,它都会影响按钮文本并且文本溢出并在按钮中被剪切。如何使按钮文字大小与按钮高度匹配?

编辑

按钮的高度设置为match_parent,因为它是父小部件

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">
            <RelativeLayout
                android:layout_marginEnd="16dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginRight="16dp">
                <Button
                    android:id="@+id/text_toolbar"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_centerInParent="true"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:textSize="36sp"
                    android:textColor="@color/white"
                    android:text="@string/k_i_n_e_c_t"/>
            </RelativeLayout>
        </android.support.v7.widget.Toolbar>

【问题讨论】:

  • 设置您的按钮高度以包裹内容,然后它会自动调整以更改文本大小。
  • 查看我所做的编辑

标签: java android xml button


【解决方案1】:

尝试像上面的代码一样将 wrap_content 属性包装到 RelativeLayout 和 Button,

  <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">
        <RelativeLayout
            android:layout_marginEnd="16dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp">
            <Button
                android:id="@+id/text_toolbar"
                style="?android:attr/borderlessButtonStyle"
                android:layout_centerInParent="true"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="36sp"
                android:textColor="@color/white"
                android:text="@string/k_i_n_e_c_t"
                android:padding="2sp"/>
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>

【讨论】:

  • 谢谢。那行得通。不得不将宽度更改为wrap_content
【解决方案2】:

只需从按钮获取高度:

button.getHeight();

设置文本大小:

button.setTextSize();

【讨论】:

  • 那行不通,因为它们的尺寸不同。此外,我需要一个 xml 修复,而不是 Java
【解决方案3】:

如果你使用这个 wrap_content 文本不会溢出

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Upload Image"
            android:id="@+id/buttonUpload" />

【讨论】:

    【解决方案4】:

    我猜你一定是用 dp 作为你的 textsize 的单位。文本尺寸最好使用 sp。

    Why should we use sp for font sizes in Android?

    【讨论】:

    • 这就是我正在使用的
    【解决方案5】:

    您可以通过设置按钮的宽度、高度和 wrap_content 来实现。

    或者您可以尝试固定按钮的大小并根据文本大小动态更改按钮大小。

    【讨论】:

      猜你喜欢
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      相关资源
      最近更新 更多