开发者

Sizing an Android multi-line TextView

开发者 https://www.devze.com 2023-01-22 03:27 出处:网络
I have an Activity where I want to display a heading that takes up the top quarter of the app and displays a dynamic set of text. In some cases it\'ll all fit on one line and I\'d like the font size t

I have an Activity where I want to display a heading that takes up the top quarter of the app and displays a dynamic set of text. In some cases it'll all fit on one line and I'd like the font size to get sized up so it's large and easy to read. In some cases it'll be on multiple lines (usually less than 4) and need to be smaller.

The complication is that if I explicitly set the newlines in setText sometimes it will insert a newline where I didn't expect one, i.e.:

"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="tr开发者_如何学Pythonue"
        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"
/>


You could count the String lenght and set the text size to fit the best size for each lenght

something in this form

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

I hope this helps.


You can do it. Append a new line charecter to the string value. Then set this string value as the text attribute of the TextView.

String temp = "ASDF \n"+"EFG \n"+"HIJ";
TextView tv=//find it from the context
tv.setText(temp);


There is one more solution that you can use WebView to do what you want check this Android TextView Justify Text answer for details. using webview you dont have to care about line alignments you can manage that by html css property


Let me recap your question to make make sure i understood you correctly.

  1. your input is always 1 to 4 lines of text
  2. you always and only want to break at the end of a line if you have more than one line
  3. you want to be each line as large as possible so that it fits the textview which is 1/4 of the height screen?

I propose that you make a custom view:

  • use Paint.getTextBounds to determine the best text size dynamically in code
  • for drawing the text you can use the helper class StaticLayout for controlling alignment and such.
  • to have you custom view take up 1/4 of the screen either put it inside a LinearLayout and use the layout_weight to assign it its height or set it programatically in View.onMeasure
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号