Android 可缩放按钮字体
Android Scalable Button Font
我想要一个尺寸的按钮,例如height = 200sp, width = 200sp,但能够根据文本的长度来缩放它的字体。我不想更改它的尺寸或将单词分成两行。我怎样才能做到这一点?
PS 我看过很多 TextView 的例子,但它们不适用于按钮 :-(
这听起来很简单。考虑这种布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:layout_width="your-width-in-dp"
android:layout_height="wrap_content"
android:text="Hello World My Friends!"
android:maxLines="1"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="2sp"
app:autoSizeMaxTextSize="100sp"
app:autoSizeStepGranularity="2sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这里是对比样张,左边是100dp,右边是200dp:
我想要一个尺寸的按钮,例如height = 200sp, width = 200sp,但能够根据文本的长度来缩放它的字体。我不想更改它的尺寸或将单词分成两行。我怎样才能做到这一点?
PS 我看过很多 TextView 的例子,但它们不适用于按钮 :-(
这听起来很简单。考虑这种布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:layout_width="your-width-in-dp"
android:layout_height="wrap_content"
android:text="Hello World My Friends!"
android:maxLines="1"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="2sp"
app:autoSizeMaxTextSize="100sp"
app:autoSizeStepGranularity="2sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这里是对比样张,左边是100dp,右边是200dp: