我有一个按钮,我为其设置了特定的高度,并让按钮文本填充整个按钮。我之前通过将 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>
最佳答案
尝试像上面的代码一样将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>
关于java - 根据按钮高度调整按钮文本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44427135/