使用按钮高度调整按钮文本大小

时间:2017-06-08 05:12:58

标签: java android xml button

我有一个按钮,我设置了一个特定的高度,按钮文本填充整个按钮。我之前通过将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> ,用于其父窗口小部件

{{1}}

5 个答案:

答案 0 :(得分:1)

如果您使用此wrap_content,则文本不会溢出

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

答案 1 :(得分: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>

答案 2 :(得分:0)

只需按下按钮的高度:

button.getHeight();

使用以下方式设置文字大小:

button.setTextSize();

答案 3 :(得分:0)

我猜你必须使用dp作为文本化的单位。最好使用sp作为文本维度。

Why should we use sp for font sizes in Android?

答案 4 :(得分:0)

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

您可以尝试修改按钮的大小,并根据文字大小动态更改按钮大小。

相关问题