• textview显示三行多余的隐藏点击按钮后显示


    首先布局
    因为我这个是列表所以这只是一个item
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_marginBottom="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:background="@drawable/rounded_box">
    
            <TextView
                android:id="@+id/tv_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="17dp"
                android:layout_marginTop="8dp"
                android:text="日期"
                android:textColor="#ff333333"
                android:textSize="13sp" />
            <TextView
               //最重要 设定显示几行
                android:maxLines="3"
    
                android:id="@+id/tv_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/tv_date"
                android:layout_alignLeft="@id/tv_date"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:text="text"
                android:textColor="#ff333333"
                android:textSize="13sp" />
            <LinearLayout
                android:id="@+id/ll_img"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="17dp"
                android:layout_below="@id/tv_text"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:layout_marginRight="15dp"
                    android:id="@+id/iv_img1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <ImageView
                    android:layout_marginRight="15dp"
                    android:id="@+id/iv_img2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <ImageView
                    android:id="@+id/iv_img3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
            <ImageView
                android:id="@+id/iv_more"
                android:visibility="gone"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="5dp"
                android:layout_below="@+id/ll_img"
                android:layout_centerHorizontal="true"
                android:src="@mipmap/more"
                android:layout_width="17dp"
                android:layout_height="12dp"/>
        </RelativeLayout>
    </LinearLayout>
    
    
    
    //java实现逻辑
    
    
    //判断文字超出三行后显示还是隐藏
        private Boolean flag = true;
    
    
    
    
     ((ViewHolderTitle) holder).tv_text.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (((ViewHolderTitle) holder).tv_text.getLineCount() >= 3) {
                            ((ViewHolderTitle) holder).iv_more.setVisibility(View.VISIBLE);
                        } else {
                            ((ViewHolderTitle) holder).iv_more.setVisibility(View.GONE);
                        }
                    }
                });
               ((ViewHolderTitle) holder).iv_more.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (flag) {
                            flag = false;
    //向上箭头图片
                            ((ViewHolderTitle) holder).iv_more.setImageResource(R.mipmap.more);
                            ((ViewHolderTitle) holder).tv_text.setEllipsize(null);//展开
                            ((ViewHolderTitle) holder).tv_text.setMaxLines(Integer.MAX_VALUE);//把TextView行数显示取消掉
                            ((ViewHolderTitle) holder).tv_text.setSingleLine(false);//这个方法是必须设置的,否则无法展开
                        } else {
                            flag = true;
    //向下箭头图片
                            ((ViewHolderTitle) holder).iv_more.setImageResource(R.mipmap.more_open);
                            ((ViewHolderTitle) holder).tv_text.setEllipsize(TextUtils.TruncateAt.END); // 收缩
                            ((ViewHolderTitle) holder).tv_text.setMaxLines(3);
                        }
                    }
                });
    

      

  • 相关阅读:
    【火炉炼AI】机器学习028-五分钟教你打造机器学习流水线
    【火炉炼AI】机器学习027-项目案例:用聚类算法建立客户细分模型
    【火炉炼AI】机器学习026-股票数据聚类分析-近邻传播算法
    【火炉炼AI】机器学习024-无监督学习模型的性能评估--轮廓系数
    【火炉炼AI】机器学习025-自动估算集群数量-DBSCAN算法
    【火炉炼AI】机器学习023-使用层次聚类算法构建模型
    【火炉炼AI】机器学习022-使用均值漂移聚类算法构建模型
    【火炉炼AI】机器学习021-使用K-means进行图片的矢量量化操作
    A Secret hdu 6153
    Fleet of the Eternal Throne HDU6138
  • 原文地址:https://www.cnblogs.com/wang-jingyuan/p/12174063.html
Copyright © 2020-2023  润新知