以字母为单位获取字符串

时间:2014-11-13 15:17:57

标签: android android-layout

我的布局如下:

|User Name      |      |
|email@mail.com | info |

如果适用于TextView,我需要设置用户的名字和第二个名称,如果当前屏幕的名字和名字太长,我需要设置名字。

例如“Tom Smith”:

|Tom Smith      |      |
|email@mail.com | info |

和“Tom VeryVeryVeryLongSurname”:

|Tom            |      |
|email@mail.com | info |

如何获取指定文本大小的名称大小并将其与TextView宽度进行比较?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="bottom"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

请参阅TextPaint#getTextBounds方法http://developer.android.com/reference/android/graphics/Paint.html#getTextBounds%28char[],%20int,%20int,%20android.graphics.Rect%29

来自TextPaintPaint

的其他内容

Paint.html#measureText似乎更符合您的要求。 http://developer.android.com/reference/android/graphics/Paint.html#measureText%28java.lang.CharSequence,%20int,%20int%29

无论如何看看android.graphics.Paint班。