問題:在 android 中向文本視圖添加文本時,當文本長度足以使文本視圖高度大于螢屏時,文本視圖只會將其高度增加到螢屏高度,而不會超出螢屏。
需求:根據文本內容增加文本視圖的高度。是否可以在運行時更改文本視圖的高度?
編輯 即使我在 XML 中更改了電視的高度(大于螢屏的高度),我的電視高度仍然沒有超過螢屏高度。
uj5u.com熱心網友回復:
MavenCentral 的 AutofitTextView 庫很好地處理了這個問題。源代碼托管在 Github(1k 星)上,網址為https://github.com/grantland/android-autofittextview
應用程式/構建。Gradle
repositories {
mavenCentral()
}
dependencies {
compile 'me.grantland:autofittextview:0.2. '
}
在代碼中啟用任何擴展 TextView 的視圖:
AutofitHelper.create(textView);
在 XML 中啟用任何擴展 TextView 的視圖:
<me.grantland.widget.AutofitLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
</me.grantland.widget.AutofitLayout>
在代碼或 XML 中使用內置 Widget:
<me.grantland.widget.AutofitTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
uj5u.com熱心網友回復:
在 Android 8.0(API 級別 26)及更高版本中,您可以指示 TextView 讓文本大小自動擴展或收縮以根據 TextView 的特征和邊界填充其布局。此設定可以更輕松地優化具有動態內容的不同螢屏上的文本大小
你的文本視圖。
<TextView
android:id="@ id/word_content_numericaltext"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:autoSizeMaxTextSize="70sp" // add this for max text size
android:autoSizeMinTextSize="30sp" // add this for min text size
android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="word"
android:textAlignment="center"
android:textColor="@color/darkblue"
android:textSize="30sp" />
注意:如果您在 XML 檔案中設定 autosizing,則不建議為 TextView 的 layout_width 或 layout_height 屬性使用值“wrap_content”。它可能會產生意想不到的結果。
有關更多詳細資訊,請參閱此處:autosizing_textview
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/311561.html
下一篇:無法決議“NetworkChangeListener”中的方法“getSupportFragmentManager”
