Android設置TextView顯示一行或多行


在listView的item中或者是特殊的業務需求中,會要求TextView的內容不完全顯示,只有通過一個指定的操作后才顯示所有的,比如說一個按鈕或者是其它的什么控件。

要想實現這個效果並不難,只要控制好TextView的行數就行。文章中介紹了兩種實現方法,一種是給button添加Flag,另一種是給button添加Tag,兩種方法都可以,具體說不上哪種更好,哪種適合用哪種。

第一種方法的布局,注意TextView中必須加上android:ellipsize和android:maxLines這兩條屬性,不然的話效果出不來:

<span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text1"
        android:layout_marginTop="10dp" >

        <TextView
            android:id="@+id/text2"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="2"
            android:text="四諦法鄺健廉四諦法鄺健廉四諦法鄺健廉四諦法鄺健廉四諦法鄺健廉四諦法鄺健廉四諦法鄺健廉四諦法鄺健廉四諦法鄺健廉四諦法鄺健廉四諦法鄺健廉四諦法鄺健廉" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginTop="10dp"
            android:text="下拉" />
    </RelativeLayout>

</RelativeLayout></span>

接下來就可以在類中控制這個TextView顯示或者隱藏了。

<span style="font-size:14px;">        button.setOnClickListener(new OnClickListener() {
            Boolean flag = true;

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (flag) {
                    flag = false;
                    text.setEllipsize(null);// 展開
                    text.setSingleLine(flag);
                    button.setText("隱藏");
                } else {
                    flag = true;
                    text.setMaxLines(2);// 收縮
                    button.setText("顯示");
                    // text.setEllipsize(TruncateAt.END);
                }
            }
        });</span>

或者可以在Button中添加一個Tag

<span style="font-size:14px;"><Button
            android:id="@+id/item_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:background="@drawable/packup"
            android:tag="true" /></span>

同樣,在代碼中獲取到改按鈕的tag進行控制

<span style="font-size:14px;">        viewItme.item_btn.setOnClickListener(new OnClickListener() {
            

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Boolean flag = Boolean.valueOf((String) viewItme.item_btn.getTag()) ;
                if (flag) {
                    // 展開
                    viewItme.item_btn.setTag("false");
                    viewItme.inspecttext_tv.setEllipsize(null);
                    viewItme.inspecttype_tv.setEllipsize(null);
                    viewItme.item_describe.setEllipsize(null);
                    viewItme.item_describe.setMaxLines(10);
                    viewItme.item_btn.setBackgroundResource(R.drawable.unfloddd);
                } else {
                    // 收縮
                    viewItme.item_btn.setTag("true");
                    viewItme.item_describe.setMaxLines(1);
                    viewItme.item_btn.setBackgroundResource(R.drawable.packup);
                }
            }
        });</span>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM