【问题标题】:How to center text inside button without altering button size in android如何在不改变android中按钮大小的情况下使按钮内的文本居中
【发布时间】:2016-03-22 06:14:45
【问题描述】:

我希望按钮内的文本适合一行,但遇到了问题:

【问题讨论】:

  • 看来你正在修复宽度或太多的填充是post xml
  • 缩小文字大小
  • 我们需要您的 xml 代码。
  • 这种尺寸的文字显然不适合这种宽度的按钮。如果你不能改变按钮的大小,唯一的办法就是让文字变小。
  • 如果按钮的宽度设置为wrap_content并且面临问题尝试添加android:singleLine="true",否则除了增加按钮的宽度或减少文本之外您别无选择尺寸

标签: android xml android-layout android-studio


【解决方案1】:

要在您的布局中做到这一点,您可以使用以下三种方式

  1. android:singleLine="true" 在按钮中。

  2. 更改字体大小

  3. 您可以减少边距

检查这个例子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll1"
    style="@style/Widget.CardContent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:background="@color/colorAccent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ll2"
        style="@style/Widget.CardContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:orientation="vertical"
        android:weightSum="2">

        <Button
            android:id="@+id/Button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="end"
            android:gravity="center"
            android:padding="5dp"
            android:singleLine="true"
            android:text="first button you must click 1"
            android:textSize="@dimen/font_normal_size"
            android:textStyle="bold" />

        <Button
            android:id="@+id/Button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="end"
            android:gravity="center"
            android:padding="5dp"
            android:singleLine="true"
            android:text="first button you must click 2"
            android:textSize="@dimen/font_normal_size"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

【讨论】:

  • 您的问题解决了吗?@Wandhaal Assan
【解决方案2】:

如果您不想像问题中提到的那样更改按钮大小,唯一的选择是尽可能减少按钮的填充并减小文本的大小。

你也可以使用

android:singleLine="true"

但这会改变按钮的大小(宽度)。

【讨论】:

    猜你喜欢
    • 2011-02-24
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多