text被挤出显示区域

有这样一种需求


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);
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,018评论 3 119
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,903评论 22 665
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,252评论 4 61
  • 相信这是很多刚刚经历完高考,或者正要经历高考的人经常听到的话:大学生活没有想象中的酷。 但这批告诉你们一点都不酷的...
    李E闪阅读 524评论 0 0
  • 开篇十一章 1、元素分类 在讲解CSS布局之前,我们需要提前知道一...
    多语阅读 213评论 0 0