简体   繁体   中英

how many string can display on full screen

I've large string, i want to split it. i got screen width and height using below code,

DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

screenHeight = metrics.heightPixels;
screenWidth = metrics.widthPixels;

I want to know how many character to display on screen.

how to calculate ? and split the string.?

On a Swing / AWT Java platform, you could use a FontMetrics object to measure the width of the particular characters you are trying to display.

References:

But it would probably be simpler to use something that can take care of the character rendering and wrapping for you.

On the Android platform, the Paint class has a number of methods that will help you do this kind of thing.

use the substring method

String substring(int startIndex, int endIndex)

split the strings per line and store them in an array of Strings

String originalString=" ....some long text " ;
String stringsByLine[]= new String(screenHeight);
int i,j=0;
for ( i = 0; j<originalString.length;i++,j++){
  stringsByLine[i]=originalString.substring(j,j+screenWidth);
  j+=screenWidth;
}

havent tried it myself, but this logic should work. :-)

i think if you set width property to wrapcontent than string automatically split and display on next line.

use this property

android:layout_width="wrap_content"
            android:layout_height="wrap_content"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM