android 实现LinearLayout布局内TextView横向排列,自动换行

/**********************************Activity里*************************************************/
/**
 * Activity窗口获得或失去焦点时被调用,在onResume之后或onPause之后
 *
 * @param hasFocus 窗口是否获取或失去焦点,true表示获取焦点,false表示失去焦点
 */
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus && isFrist) {
        isFrist = false;

        // 获取Layout布局
        ViewGroup container = findViewById(R.id.ll_read);
        this._draw(container, readList);
    }
}
/**
 * 绘制TextView显示班级成员阅读情况
 *
 * @param container 容器
 * @param list      集合
 */
private void _draw(ViewGroup container, List<String> list) {

    try {
        // 获取container的宽度
        final int containerWidth = container.getMeasuredWidth();
        final int containerHeight = container.getMeasuredHeight();
        final LayoutInflater inflater = getLayoutInflater();

        // 用来测量字符的宽度
        final Paint paint = new Paint();
        final TextView textView = (TextView) inflater.inflate(R.layout.activity_text, null);
        final int itemPadding = textView.getCompoundPaddingLeft() + textView.getCompoundPaddingRight();
        final LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        tvParams.setMargins(0, 0, 0, 0);

        paint.setTextSize(textView.getTextSize());

        // 行layout
        LinearLayout layout = new LinearLayout(this);
        layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        layout.setOrientation(LinearLayout.HORIZONTAL);
        container.addView(layout);

        final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, 0, 0);

        // 行layout设置(高 30 dp)
        int childLayoutHeight = Util.dip2px(this, 30);
        final LinearLayout.LayoutParams paramsChildLayout = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, childLayoutHeight);
        // 一行剩下的空间
        int remainWidth = containerWidth;
        // 总高度
        int itemHeightTotal = 0;

        for (int i = 0; i < list.size(); ++i) {
            final String text = list.get(i);
            final float itemWidth = paint.measureText(text) + itemPadding;
            TextView tvItem = (TextView) inflater.inflate(R.layout.activity_text, null);
            tvItem.setText(text);

            if (i == 0) {
                itemHeightTotal = (int) (childLayoutHeight + 0.5f);
            }

            if (remainWidth > itemWidth) {
                layout.addView(tvItem);
            } else {
                layout = new LinearLayout(this);
                layout.setLayoutParams(paramsChildLayout);
                layout.setOrientation(LinearLayout.HORIZONTAL);

                // 将前面那一个textview加入新的一行
                layout.addView(tvItem);
                container.addView(layout);
                remainWidth = containerWidth;

                itemHeightTotal = itemHeightTotal + (int) (childLayoutHeight + 0.5f);
            }
            // 剩余宽度计算
            remainWidth = (int) (remainWidth - itemWidth + 0.5f);

            // 计算高度是否超出设定的高度500px,超出后设置高度为可适应性高度
            if (itemHeightTotal >= containerHeight) {
                container.setLayoutParams(params);
            }
        }
    } catch (Exception e) {
        Log.e("异常", "_draw: ", e);
    }
}

 

/******************************************页面xml*****************************************/

<!-- activity_text.xml -->
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingTop="10dp"
    android:text=""></TextView>

/*****************************************运行结果*****************************************/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程某某某

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值