安卓:渲染前观测量尺寸 [英] Android: Measure Size of View before rendering

查看:124
本文介绍了安卓:渲染前观测量尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示改变和运行时的文字一个TextView。我想用View.getLineCount()方法来TextView的多少行占用,并相应做不同的功能。问题是我需要确定TextView的在需要知道有多少线占用相同的方法占用的行数。我已经打过电话View.invalidate()在两个电话之间但这似乎并没有任何修复。我想衡量的行数勿使如果可能的观点,但如果我要使它我将开放给做的这一点。让我知道如果这是不够具体,我会尽量更具体。谢谢!

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!

推荐答案

这其实是可以在Android中一个非常复杂的问题。只有当你需要知道你设置文本(即前视图显示或附近被渲染)的时候,你是真正的选择是使用StaticLayout。如果你能等待,直到接近渲染时间,你可以使用一个viewTreeObserver:

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可称为多次,有​​时(无论何种原因),我发现,意见没有实际布设了没有,所以你应该做一些记录,看看是否能找到一种方法,以确保无论结果你实际上得到有意义(即行数> 0)。

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是一个更复杂的替代方案,应该能够给你的看法和行数的大小。他们可能需要一些调整来获得正常工作,并确保您使用的油漆从你试图测量的看法:

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();

如果你愿意(或已经)的子类的TextView,您可以覆盖onSizeChanged或onMeasure并试图让行数那里。同样,像与视图树观察者,有时那些可你的观点实际上已经测量之前调用,所以你可能要检查,以确保您在这些方法中得到什么价值意义。

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.

这篇关于安卓:渲染前观测量尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆