TextView 判断自动换行

来源:互联网 发布:哪个淘宝客插件最牛 编辑:程序博客网 时间:2024/05/29 10:52

先看 需求: 布局中有四种样式 (标签必须在一起 不能截断) 因为没办法用字段区分这四种类型, 所以只能用一个item布局实现效果

UI效果图

原理是这样的, 主要是中间的内容 跟后面的标签 会出现这样的问题, 项目中解决的方案是 用两个TextView : content 和 label , content动态计算, 计算换行逻辑: content +label 一行 或者 label不截断的情况下两行 或者 content两行 label一行,,,,,, 当不需要换行的是后 就用一个content 另一个label组件隐藏, 把内容拼接到content中去,, 当需要换行的时候就打开label标签

直接上代码:

//核心代码 这个方法: private boolean isNewLine(final TextView textView, String text, final TextView extTextView, String appendText, float lineWidth, float ratio, int maxLine) {        if (TextUtils.isEmpty(appendText.trim())) {            return false;        }        final Paint tvPaint = textView.getPaint();        tvPaint.setTextSize(ctrip.foundation.util.DeviceUtil.getPixelFromDip(16));        float contentWidth = tvPaint.measureText(text);        float sapWidth = tvPaint.measureText(" ");        final Paint tvExtPaint = extTextView.getPaint();        tvPaint.setTextSize(ctrip.foundation.util.DeviceUtil.getPixelFromDip(12));        float extContentWidth = tvExtPaint.measureText(appendText);// 空格 + 标签 +空格 +标签        if (extContentWidth >= lineWidth || contentWidth >= lineWidth * maxLine || ((contentWidth + extContentWidth) > maxLine * lineWidth)) {            return true;        } else {            float lessWidth = (lineWidth * ratio - contentWidth % lineWidth - sapWidth);            if (lessWidth < extContentWidth) {                return true;            }        }        return false;    }//调用isNewLine方法的位置if (((ItemNormalVH) holder).tv_price_item_new_search.getVisibility() == View.GONE && ((ItemNormalVH) holder).tv_totalCountRange_item_new_search.getVisibility() == View.GONE) {                suggestTvWidth = ctrip.foundation.util.DeviceUtil.getScreenWidth() - ctrip.foundation.util.DeviceUtil.getPixelFromDip(58);//15+20+8+15//                Log.i(TAG, "屏幕宽度: "+ ctrip.foundation.util.DeviceUtil.getScreenWidth()+" , 已经占用"+ctrip.foundation.util.DeviceUtil.getPixelFromDip(58));            } else {                TextView textView=((ItemNormalVH) holder).tv_price_item_new_search.getVisibility() == View.VISIBLE ? ((ItemNormalVH) holder).tv_price_item_new_search:((ItemNormalVH) holder).tv_totalCountRange_item_new_search;                String text=((ItemNormalVH) holder).tv_price_item_new_search.getVisibility() == View.VISIBLE ?mList.get(position).price+"起":mList.get(position).totalCountRange;                final Paint tvPaint = textView.getPaint();                float rightContentWidth = tvPaint.measureText(text);                suggestTvWidth = ctrip.foundation.util.DeviceUtil.getScreenWidth() - (ctrip.foundation.util.DeviceUtil.getPixelFromDip(58)+rightContentWidth);//                Log.i(TAG, "屏幕宽度: "+ ctrip.foundation.util.DeviceUtil.getScreenWidth()+" , 已经占用"+(ctrip.foundation.util.DeviceUtil.getPixelFromDip(58)+rightContentWidth));            }            SpannableStringBuilder ssb = new SpannableStringBuilder();            ssb.append(setStyleName(mList.get(position).name));            SpannableStringBuilder ssbExt = new SpannableStringBuilder();            ssbExt.append(setStyleLabel(mList.get(position).extension, mList.get(position).districtName + "  " + mList.get(position).distance));            boolean newLine = isNewLine(((ItemNormalVH) holder).tv_name_item_new_search, ssb.toString(), ((ItemNormalVH) holder).tv_label_item_new_search, ssbExt.toString(), suggestTvWidth, 0.9f, 2);            ((ItemNormalVH) holder).tv_name_item_new_search.setMaxLines(2);            ((ItemNormalVH) holder).tv_name_item_new_search.setEllipsize(TextUtils.TruncateAt.END);            if (newLine  && !TextUtils.isEmpty(ssbExt.toString().trim())) {                ((ItemNormalVH) holder).tv_name_item_new_search.setText(ssb);                ((ItemNormalVH) holder).tv_label_item_new_search.setVisibility(View.VISIBLE);                ((ItemNormalVH) holder).tv_label_item_new_search.setText(ssbExt);            } else {                ((ItemNormalVH) holder).tv_label_item_new_search.setVisibility(View.GONE);                if (ssbExt.length() > 0 && !TextUtils.isEmpty(ssbExt.toString().trim())) {                    ssb.append("  ").append(ssbExt);                }                ((ItemNormalVH) holder).tv_name_item_new_search.setText(ssb);            }//布局文件<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"                xmlns:app="http://schemas.android.com/apk/res-auto"                xmlns:tools="http://schemas.android.com/tools"                android:id="@+id/rl_item_new_search"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:paddingTop="12dp"                android:gravity="center_vertical"                android:layout_marginLeft="15dp"                android:layout_marginRight="15dp"                android:orientation="vertical">    <ctrip.android.basebusiness.ui.svg.SVGImageView        android:id="@+id/iv_item_new_search"        android:layout_width="20dp"        android:layout_height="20dp"        android:layout_alignParentLeft="true"        android:contentDescription="@null"        android:padding="@dimen/dimen_2dp"        app:svgPaintColor="#fff"        android:layout_marginTop="3dp"        app:svgSrc="@raw/gs_des"/>    <RelativeLayout        android:id="@+id/content_linearlayout"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="8dip"        android:paddingTop="@dimen/dimen_2dp"        android:layout_toLeftOf="@+id/tv_price_item_new_search"        android:layout_toRightOf="@id/iv_item_new_search"        >        <!--这里是content-->        <TextView            android:id="@+id/tv_name_item_new_search"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="left"            android:lineSpacingExtra="1dip"            android:lineSpacingMultiplier="1.1"            android:maxLines="1"            android:text="大丰麋鹿国家级自然保护区"            android:textColor="#333333"            android:textSize="15dip"/><!--这里是label 如果需要换行就显示-->        <TextView            android:id="@+id/tv_label_item_new_search"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_below="@+id/tv_name_item_new_search"            android:ellipsize="end"            android:gravity="left"            android:lineSpacingExtra="1dip"            android:lineSpacingMultiplier="1.1"            android:layout_marginTop="5dp"            android:singleLine="true"            android:text="将是多空临界点克里斯尽快落实"            android:textColor="#666666"            android:textSize="13dip"            android:visibility="gone"/>    </RelativeLayout>    <TextView        android:id="@+id/tv_price_item_new_search"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_centerVertical="true"        android:maxLines="1"        android:gravity="right"        android:text="9.9元起"        android:visibility="gone"        android:textColor="#ff6633"        android:textSize="15sp"/>    <TextView        android:id="@+id/tv_totalCountRange_item_new_search"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_centerVertical="true"        android:maxLines="1"        android:gravity="right"        android:visibility="gone"        android:text="259项"        android:textColor="#666"        android:textSize="13sp"/>    <View        android:id="@+id/line"        android:layout_width="match_parent"        android:layout_height="0.2dp"        android:layout_marginTop="12dp"        android:layout_below="@+id/content_linearlayout"        android:background="#eaeaea"/></RelativeLayout>
原创粉丝点击