/**********************************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>
/*****************************************运行结果*****************************************/