在android中检查是否有字符串从我的视图溢出?

在android中检查是否有字符串从我的视图溢出?,android,android-layout,android-listview,textview,Android,Android Layout,Android Listview,Textview,我在Android中有一个动态加载的listview。列表视图包含一个图像,其上显示图像的描述,理想情况下,该图像应占据图像高度的20%。如何检查列表中位于图像视图顶部的文本视图的最大高度是否为图像的20% 正如你从截图上看到的,“哈维店--登记一下,就有机会赢取一个免费汉堡 Picker,ON“在文本视图中有两行,有时如果我的android设备很小,它会超出背景(黑色)视图,看起来很糟糕。我需要停止这样做,所以我决定如果字符串在我的textview上溢出,我必须截断它 如何截断textvie

我在Android中有一个动态加载的listview。列表视图包含一个图像,其上显示图像的描述,理想情况下,该图像应占据图像高度的20%。如何检查列表中位于图像视图顶部的文本视图的最大高度是否为图像的20%

正如你从截图上看到的,“哈维店--登记一下,就有机会赢取一个免费汉堡 Picker,ON“在文本视图中有两行,有时如果我的android设备很小,它会超出背景(黑色)视图,看起来很糟糕。我需要停止这样做,所以我决定如果字符串在我的textview上溢出,我必须截断它

如何截断textview上的字符串,以便在达到imageview高度的20%后,图像描述以“…”结尾

下面给出了我目前拥有的xml。请注意,com.parse.myPackage.AspectRatioImageView只是imageview的自定义视图

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFFFF" >

<com.parse.myPackage.AspectRatioImageView
    android:id="@+id/campaignImageLabel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:src="@drawable/blank" />

<FrameLayout
    android:id="@+id/frameLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/frameLayout2"
    android:layout_alignParentLeft="true"
    android:background="@color/fadedBlack">

        <TextView
            android:id="@+id/campaignNameLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Loading..."
            android:layout_marginLeft="10dp"
            android:textColor="@color/white"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textStyle="bold" />



</FrameLayout>

<FrameLayout
    android:id="@+id/frameLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/campaignImageLabel"
    android:layout_alignParentLeft="true"
    android:background="@color/fadedBlack">

        <TextView
        android:id="@+id/campaignLocationLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="Location Loading..."
        android:textColor="@color/white"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />
</FrameLayout>


在您的文本视图上尝试此操作

android:maxLines="2"
android:ellipsize="end"
如果文本长度超过2行,这将截断文本并在结尾处放置“…”

您可以使用:

android:ellipsize="end" 
android:maxLines="1"

更改
maxlines
的值以手动检查它是否为imageview高度的20%<代码>省略将处理“…”终止

我这样做了,它在1行之后截断了我的文本,但是我没有像预期的那样在结尾处得到“…”。我在第一行有“你好,我叫阿克沙和”,在第二行有“我是来和你说话的”。第一行正在被截断,但是现在。。。“阿克沙和”问题解决后,你是对的,这是因为我第一行的最后一句话是完整的word@AkshatAgarwal很高兴我能帮助你,你应该把这个标记为答案…你的答案和另一个答案一样正确,但是另一个答案比你的答案早3分钟回答,因此我给他做了标记。我这样做了,我截短了两行后的文本,但是我遗漏了第二行末尾的“…”。有什么想法吗?我的文本过去正好有两行,第三行的开头是“点击这里”。我的“点击这里”没有显示,但是在这个问题解决之前没有“…”,这是因为我第二行的最后一个单词是一个完整的单词,它不必截断“…”将不会显示文本是否适合两行。我猜您正在代码中设置“并单击此处”文本,它是同一文本视图的一部分。如果是这样的话,最好在android上为它提供一个单独的文本视图:visibility=“GONE”。然后在加载所有内容时使其在代码中可见。