English 中文(简体)
Android: Measure Size of View before rendering
原标题:

I have a TextView that displays text that changes and run time. I want to use the View.getLineCount() method to see how many lines the TextView takes up and do different functions accordingly. The problem is I need to determine the number of lines the TextView takes up in the same method that needs to know how many line it takes up. I have tried calling View.invalidate() in between the two calls but that hasn t seemed to fix anything. I would like to measure the number of lines without rendering the view if possible but if I have to render it I would be open to doing that as well. Let me know if this is not specific enough and I will try to be more specific. Thanks!

问题回答

This can actually be a really complex problem in Android. If you need to know at the time you set the text (ie before the view is rendered or near being rendered), you re only real option is to use StaticLayout. If you can wait until near render time, you can use a viewTreeObserver:

yourTextView.getViewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout(){
        yourTextView.getLineCount();
    }
}

onGlobalLayout can be called multiple times and sometimes (for whatever reason) I ve found that views aren t actually laid out yet, so you should do some logging and see if you can find a way to make sure that whatever result you get actually makes sense (ie a line count > 0).

StaticLayouts are a more complicated alternative that should be able to give you the size of your view and number of lines. They can require some tweaking to get to work properly, and make sure you use the paint from the view you re trying to measure:

yourTextView.setText(newText);
//the last boolean should be true if you re using padding on your view
StaticLayout measure = new StaticLayout(yourTextView.getText(), yourTextView.getPaint(), 
    maxAvailableWidthForYourTextView, Layout.Alignment.ALIGN_NORMAL, 1.0f, 1.0f, false)

int numberOfLines = measure.getLineCount();

If you re willing to (or already have) subclassed TextView, you can override onSizeChanged or onMeasure and try to get the number of lines there. Again, like with the view tree observer, sometimes those can be called before your view has actually been measured, so you may have to check to make sure whatever value you get in those methods make sense.





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签