美文网首页bug集
text被挤出显示区域

text被挤出显示区域

作者: 烬日沉香 | 来源:发表于2018-07-28 17:44 被阅读215次

有这样一种需求


image.png

限定一行展示商品名称和商品的价格。当商品名称过长时,价格则有可能显示不下。
需求是:商品价格展示第一优先,商品名称text则填充剩余位置。
那么这样写肯定不行的,需要在代码中动态修改商品text的大小

<LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"  >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    android:ellipsize="end"
                    android:text="榴莲香雪新鲜榴莲生日蛋糕"
                    />
                <TextView
                    android:id="@+id/tv_item_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    android:text="¥43.0"
                    android:paddingLeft="@dimen/dp_17"
                    />
            </LinearLayout>

实现方式:给名称和价格的text都添加布局改变时的监听, addOnGlobalLayoutListener。
当在一个视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变时,会触发该回调。

tvItemName.getViewTreeObserver().addOnGlobalLayoutListener(skuTitleLayoutListener);
tvItemPrice.getViewTreeObserver().addOnGlobalLayoutListener(skuTitleLayoutListener);
tvItemName.getViewTreeObserver().addOnGlobalLayoutListener(skuTitleLayoutListener);
tvItemPrice.getViewTreeObserver().addOnGlobalLayoutListener(skuTitleLayoutListener);
 /**
     * 调整名称和价格的位置,以价格优先展示完整,名称以缩略形式展示
     */
    private ViewTreeObserver.OnGlobalLayoutListener skuTitleLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        int maxLines = 0;
        int children = 0;
        View textName;
        View textPrice;
        int marginLeft = 17;

        @Override
        public void onGlobalLayout() {
            children = 0;
            //获取左边对齐留出的距离
            if (context != null) {
                marginLeft = (int) context.getResources().getDimension(R.dimen.dp_17);
                float dm = DeviceUtil.getDensityFromDevice(context);
                marginLeft = (int) (dm * marginLeft);
            } else {
                marginLeft = (int) (marginLeft * 1.5);
            }
            //计算留给名称和价格展示的长度
            maxLines = (llSkuTitle.getWidth() - marginLeft);
            for (int i = 0; i < llSkuTitle.getChildCount(); i++) {
                //获取名称text占据的宽度
                View view = llSkuTitle.getChildAt(i);
                children += view.getWidth();
                if (i == 0) {
                    textName = view;
                } else {
                    textPrice = view;
                }
            }
            //判断子view的总长度是否超过最大长度,若超过,则重新调整,限定名称text的宽度
            if (children > maxLines) {
                int width = maxLines - textPrice.getWidth();
                LinearLayout.LayoutParams textNameParams = new LinearLayout.LayoutParams(
                        width, LinearLayout.LayoutParams.WRAP_CONTENT);
                textName.setLayoutParams(textNameParams);
                LinearLayout.LayoutParams textPriceParams = new LinearLayout.LayoutParams(LinearLayout.
                        LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                textPrice.setLayoutParams(textPriceParams);
            }
        }
    };

移除监听,需适配版本

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
 tvItemPrice.getViewTreeObserver().removeOnGlobalLayoutListener(skuTitleLayoutListener);
   tvItemName.getViewTreeObserver().removeOnGlobalLayoutListener(skuTitleLayoutListener);
} else {
   tvItemPrice.getViewTreeObserver().removeGlobalOnLayoutListener(skuTitleLayoutListener);
   tvItemName.getViewTreeObserver().removeGlobalOnLayoutListener(skuTitleLayoutListener);
}

相关文章

  • text被挤出显示区域

    有这样一种需求 限定一行展示商品名称和商品的价格。当商品名称过长时,价格则有可能显示不下。需求是:商品价格展示第一...

  • 鸿蒙 Text 控件的各种用法

    Text Text是用来显示字符串的组件,在界面上显示为一块文本区域。Text作为一个基本组件,有很多扩展,常见的...

  • swift UILabel多行显示时 计算UILable的高度(

    代码如下 其中text为需要显示的字符串,font为字体大小的设置,width为显示区域的宽度 如下:

  • iOS UITextField

    UITextField:在界面中显示可编辑文本区域的对象 UITextField的属性和方法 1. text:设置...

  • clip-path属性(CSS)

    MDN 属性可以创建一个只有元素的部分区域可以显示的剪切区域。区域内的部分显示,区域外的隐藏。剪切区域是被引用内嵌...

  • 十七、文字区域Text

    文字区域Text的基本概念 Text的构造方法如下。Text(父对象,options,···) Text()方法的...

  • ReactNative Text最后一位被截断

    今天同事碰到一个神奇的问题:一加手机上,显示日期的Text组件,最后一位被截断没有显示。他说是Text的样式设置了...

  • 《图解CSS3》学习记录(03-文本省略与换行)

    文本省略: 通常在页面有限区域要显示较多内容的时候,可以使用文本省略。 使用text-overflow:ellip...

  • 2 文本及样式

    原文在此,此处只为学习 Text Text文本属性如下所示 Text用于显示简单样式文本,它包含一些控制文本显示样...

  • Flutter组件

    文本及样式 Text: 显示简单样式文本TextStyle: 指定文本显示的样式TextSpan: 对一个Text...

网友评论

    本文标题:text被挤出显示区域

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