在运行code TextView.measure()时,为什么我得到不同的结果,从我看到呈现在屏幕上 [英] Why I'm getting different results when running TextView.measure() in code, from what I see rendered on screen

查看:232
本文介绍了在运行code TextView.measure()时,为什么我得到不同的结果,从我看到呈现在屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正试图在某种程度上的TextView重叠文本来解决问题。
我已经发布了有关该问题的,但我也试图解决它自己。我决定来算文本导致TextView的突破线路的数量。我想出了这个糙米code,基本上应膨胀的观点,设置它的布局PARAMS根据显示的大小,然后onmeasure运行和返回lineCount。我计划比可能使用二进制搜索以找到适合TextView的确切的文本长度,但即使以前我只是试着运行code,看看它是如何工作。这是一种奇怪的,因为它给了我不同的结果,比我在屏幕上看到比。我甚至尝试按比例密度改变TEXTSIZE,因为我不知道是否是被考虑的。

下面是code。它返回三个,但是当我渲染布局,屏幕上的文字只占用两行。

 公众诠释getCountOfLines(上下文的背景下,诠释widgetDp){    LayoutInflater mLayoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    的LinearLayout布局=(的LinearLayout)mLayoutInflater.inflate(R.layout.event_basic_large,空,假);    TextView的titleTextView =(TextView中)layout.findViewById(R.id.itemTitle);
    titleTextView.setText(TEST_TEXT);    浮密度= context.getResources()getDisplayMetrics()密度。
    浮textDensity = context.getResources()getDisplayMetrics()scaledDensity。;    titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,titleTextView.getTextSize()/ *密度textDensity);    layout.setLayoutParams(
            新LinearLayout.LayoutParams(
                    (中间体)(widgetDp *密度),
                    ViewGroup.LayoutParams.WRAP_CONTENT)
    );
    layout.measure(View.MeasureSpec.makeMeasureSpec(
            (中间体)(widgetDp *密度),View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED)
    );    Log.d(TAG,titleTextView.getLineCount()+线宽度+ titleTextView.getMeasuredWidth());
    返回titleTextView.getLineCount();
}


解决方案

我遇到了这个前阵子和搜索,终于尝试后得到了下面的函数来工作:

 公众诠释getLineCount(字符串的TestString,浮TEXTSIZE,
        浮动宽度){
    矩形边界=新的矩形();
    涂料粉刷=新的油漆();
    paint.setTypeface(LT; FONT>); //设置您正在使用这种字体
    paint.setTextSize(TEXTSIZE);
    paint.getTextBounds(的TestString,0,testString.length(),边界);    回报(INT)Math.ceil(bounds.width()/宽);
}

此功能使用TEXTSIZE和宽度给行数。
其显示之前此功能给于一个TextView行的行号。

I'm now trying to resolve an issue with somehow overlapping text in TextView. I've posted a question about that , but I've also tried to solve it myself. I decided to count the amount of text which causes the textview to break the line. I came up with this unpolished code, which basically should inflate the view, set it's layout params according to the displayed size and then run onmeasure and return lineCount. I plan than to use probably binary search to find exact text length which fits into the textview, but even before I've just tried to run the code and see how it behaves. It's kind of weird, because it gives me different results, than what I see on screen than. I even tried to alter the textsize according to scaled density, because I wasn't sure whether it's been taken into account.

Here is the code. It returns three,but when I render the layout to screen the text takes up only two lines.

public int getCountOfLines(Context context, int widgetDp){

    LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout = (LinearLayout) mLayoutInflater.inflate(R.layout.event_basic_large ,null, false);

    TextView titleTextView = (TextView) layout.findViewById(R.id.itemTitle);
    titleTextView.setText(TEST_TEXT);

    float density = context.getResources().getDisplayMetrics().density;
    float textDensity = context.getResources().getDisplayMetrics().scaledDensity;

    titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextView.getTextSize()/density*textDensity);

    layout.setLayoutParams(
            new LinearLayout.LayoutParams(
                    (int) (widgetDp*density),
                    ViewGroup.LayoutParams.WRAP_CONTENT)
    );


    layout.measure(View.MeasureSpec.makeMeasureSpec(
            (int) (widgetDp*density), View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
    );

    Log.d(TAG, titleTextView.getLineCount() + " lines, width "+titleTextView.getMeasuredWidth());


    return titleTextView.getLineCount();
}

解决方案

I ran into this a while back and after searching and trying finally got the following function to work:

public int getLineCount(String testString, float textSize, 
        float width) {
    Rect bounds = new Rect();
    Paint paint = new Paint();
    paint.setTypeface(<font>);//set font that you are using for this
    paint.setTextSize(textSize);
    paint.getTextBounds(testString, 0, testString.length(), bounds);

    return (int) Math.ceil(bounds.width() / width);
}

This function uses textSize and width to give number of lines. This functions give number of line in a textview before it is displayed.

这篇关于在运行code TextView.measure()时,为什么我得到不同的结果,从我看到呈现在屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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