美文网首页
##TextView 行末...详情实现

##TextView 行末...详情实现

作者: 小天1106 | 来源:发表于2018-01-26 15:07 被阅读0次

TextView 行末...详情实现

作为Android开发人员,会经常遇到产品针对TextView显示的各种需求,其中有一种就是在TextView尾部拼接 ...详情或者...更多。有些是第一行后边,有些是第二行第三行后边。但是我们并不知道TextView每行会放多少个字,这就导致我们不知道应该截取多少个字符,在TextView上显示,然后拼接我们所需要的...详情。

目前看到网上有些做法就是计算TextView占据的宽度,然后逐个字符的去计算占据的宽度,然后填充。经研究发现,其实Android中已经封装好了这写。具体代码如下。
布局文件如下:
、、、
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

            android:layout_width="match_parent"
            android:layout_height="match_parent">
<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:text=""
    android:layout_height="wrap_content"/>
<TextView
    android:id="@+id/first_line"
    android:text="第一行"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_below="@+id/first_line">
<TextView
    android:id="@+id/second_line"
    android:text="第二行"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<TextView
    android:id="@+id/text_detail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:layout_alignParentRight="true"
    android:layout_marginRight="4dp"
    android:textColor="#0000ff"
    android:visibility="visible"
    android:text="...详情"/>
</RelativeLayout>

</RelativeLayout>

设计代码:
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.texttest);
    textView = (TextView) findViewById(R.id.text);
    firstLineText=(TextView)findViewById(R.id.first_line);
    secondLineTest=(TextView)findViewById(R.id.second_line);
    String popRmkUp = "由入境免签证计划前往美国旅游或过境美国的旅客必需携带护照,去往港澳的旅客需要携带港澳通行证祝你旅途愉快,下次再来。";
    textView.setText(popRmkUp);
   textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Layout layout = textView.getLayout();
            int lines = textView.getLineCount();
            //为了转换String 到 StringBuilder
            StringBuilder SrcStr = new StringBuilder(textView.getText().toString());
            String firstLine,secondLine;
            if (lines > 2) {
                //使用getLineStart 和 getLineEnd 得到指定行的开始和结束的坐标,坐标范围是SrcStr整个字符串范围内。
                firstLine = SrcStr.subSequence(layout.getLineStart(0), layout.getLineEnd(0)).toString();//第一行
                secondLine = SrcStr.subSequence(layout.getLineStart(1), layout.getLineEnd(1)).toString();//第二行
                firstLineText.setText(firstLine);
                secondLineTest.setText(secondLine.substring(0,secondLine.length()-3));
                findViewById(R.id.text_detail).setVisibility(View.VISIBLE);
                textView.setVisibility(View.GONE);
                textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        }
    });
}

通过TextView的layout就可以获取我们想要的一切信息,按照上述代码,就可以实现TextView 末尾添加诸如...详情或者图片的任何信息。

相关文章

网友评论

      本文标题:##TextView 行末...详情实现

      本文链接:https://www.haomeiwen.com/subject/huiiaxtx.html