我是靠谱客的博主 发嗲蜡烛,最近开发中收集的这篇文章主要介绍textView中文本过长被截断的解决方案,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

原问题来自于CSDN问答频道,更多解决方案见:http://ask.csdn.net/questions/1840

问题描述:

有一个多行文本的textView,如果文本不超过显示空间,就设置重力向上

tv.setGravity(Gravity.TOP);


如果文本过多超出范围,我需要显示文本的结尾部分。

tv.setGravity(Gravity.BOTTOM);


布局:

<RelativeLayout
    android:id="@+id/textLayout"
    android:layout_marginTop="16dp"
    android:layout_width="fill_parent"
    android:layout_height="110dp"
    android:orientation="vertical"
    android:visibility="visible" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="top"
        android:text="Long text bla bla."
        android:textColor="@android:color/black"
        android:textColorHint="@android:color/black"
        android:textSize="37sp" />
   ...
</RelativeLayout>


不知道应该怎么让应用能判断文本有多长?有没有超出范围。每个屏幕尺寸不同所以不能通过硬编码设定行数判断。

解决方案:

LinearLayout中加入TextVew,然后比较TextVew 和linearlayout 的高度

<RelativeLayout
        android:id="@+id/textLayout"
        android:layout_marginTop="16dp"
        android:layout_width="fill_parent"
        android:layout_height="110dp"
        android:orientation="vertical"
        android:visibility="visible" >

<LinearLayout 
        android:id ="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:gravity="top"
            android:text="Long text bla bla."
            android:textColor="@android:color/black"
            android:textColorHint="@android:color/black"
            android:textSize="37sp" />


比较高度:

if(text.getHeight()>linear.getHeight()){
            //Here TextView longer than available space
        }


 

最后

以上就是发嗲蜡烛为你收集整理的textView中文本过长被截断的解决方案的全部内容,希望文章能够帮你解决textView中文本过长被截断的解决方案所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(34)

评论列表共有 0 条评论

立即
投稿
返回
顶部