检测裁剪Android中的TextViewAndroid、TextView

2023-09-05 04:19:16 作者:一抹胭脂一抹白

我在我的Andr​​oid应用程序上有一组宽度一个TextView。目前它得到了center_horitonzal和一组TEXTSIZE(9SP)重力。我拉值来把这个标签从SQLite数据库,以及一些值过大,在当前的TEXTSIZE以适应TextView中。

有没有一种方法来检测一个TextView中的文本将被裁剪?我想这个检测,并降低了字体,直到它适合。有一个在iPhone的UILabel处理这个名为adjustToFit(其中也有一个最小字体大小)一个不错的属性,我基本上试图仿效这一点。

下面是我的工作与TextView中的一个例子:

 < TextView的机器人:ID =@ + ID / label5机器人:layout_width =62px
            机器人:layout_height =WRAP_CONTENT机器人:layout_x =257px
            机器人:layout_y =169px机器人:TEXTSIZE =9SP
            机器人:文字颜色=#000
            机器人:字体=SANS的Andr​​oid版本:TEXTSTYLE =黑体
            机器人:重力=center_horizo​​ntal机器人:行=1/>
 

解决方案

我找到了一种方法来测量使用的TextView的Paint对象文本的宽度,降低它,直到它在我需要的大小适合。下面是一些示例code:

 浮动大小= label.getPaint()measureText(item.getTitle())。
    而(尺寸与GT; 62){
        浮newSize = label.getTextSize() -  0.5F;
        label.setTextSize(newSize);
        大小= label.getPaint()measureText(item.getTitle())。
    }
 

android studio怎么使imageview

I have a TextView in my android application that has a set width on it. It's currently got a gravity of "center_horitonzal" and a set textSize (9sp). I pull values to put on this label from a sqlite database, and some of the values are too large to fit in the TextView at the current textSize.

Is there a way to detect that the text inside a TextView is going to be clipped? I'd like to detect this, and lower the font until it fits. There is a nice property in the iPhone UILabel that handles this called "adjustToFit" (which also has a minimum font size), and I'm basically trying to emulate that.

Here's an example of the TextView that I'm working with:

<TextView android:id="@+id/label5" android:layout_width="62px"
            android:layout_height="wrap_content" android:layout_x="257px"
            android:layout_y="169px" android:textSize="9sp" 
            android:textColor="#000"
            android:typeface="sans" android:textStyle="bold"
            android:gravity="center_horizontal" android:lines="1" />

解决方案

I found a way to measure the width of text using the TextView's Paint object, and lower it until it fit in the size I needed. Here's some sample code:

    float size = label.getPaint().measureText(item.getTitle());
    while (size > 62) {
        float newSize = label.getTextSize() - 0.5f;
        label.setTextSize(newSize);
        size = label.getPaint().measureText(item.getTitle());
    }