gpt4 book ai didi

android - 调整 Android 多行 TextView 的大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:15:32 26 4
gpt4 key购买 nike

我有一个 Activity,我想在其中显示占据应用程序顶部四分之一的标题并显示一组动态文本。在某些情况下,它会全部放在一行上,我希望字体大小可以变大,这样它就大了并且易于阅读。在某些情况下,它将位于多行(通常少于 4 行)并且需要更小。

复杂的是,如果我在 setText 中明确设置换行符,有时它会在我没有预料到的地方插入一个换行符,即:

"My first line of text\nMy Second line of text"Might get turned into (it's right adjusted in this version):"My first line of             text   My second line          of text"

I'm not sure how to go about either keeping the TextView from trying to insert newlines or letting it just take care of formatting the whole thing. If I set some text to be one really long single word it'll insert a newline in the middle at some chosen point. And other times I just sort of randomly get the last few lines of text.

Should I just explicitly set the font size to be really small to get it laid out correctly and then manually adjust the size? Is there a 'best practice' that I should use for this? If I set the font size to be a reasonably large font it kind of works but it would be nice to figure out what the 'right' way to do this would be. And I'd rather not have to manually add in 4 single line TextViews and format them myself.

The layout xml I'm using is:

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dip"
android:layout_marginRight="8dip"
android:singleLine="false"
android:ellipsize="marque"
android:gravity="right|bottom"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="30sp"
/>

最佳答案

您可以计算字符串长度并设置文本大小以适合每个长度的最佳大小

这种形式的东西

if(myString.length()==40){
myTextview.setTextSize(11);
else if(myString.length()==25){
myTextview.setTextSize(16);
...

希望对您有所帮助。

关于android - 调整 Android 多行 TextView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4001257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com