这是我自己封装的一个工具类,可以拿来就可以使用了。值需要在构造方法中传入相应的类容就可以了
public class LinearAddUtil {
int resourceId;
ViewGroup container;
Context context;
List list = new ArrayList<>();
int lineMargin = 50;
int itemMargin = 50;
public LinearAddUtil(int resourceId, ViewGroup container, List list, Context context, int lineMargin, int itemMargin) {
this.resourceId = resourceId;
this.container = container;
this.list = list;
this.context = context;
this.lineMargin = lineMargin;
this.itemMargin = itemMargin;
init();
}
public void init() {
//获取宽度
int containerWidth = container.getMeasuredWidth() - container.getPaddingLeft() - container.getPaddingRight();
TextView textView = (TextView) LayoutInflater.from(context).inflate(resourceId, null);
//获取textView的padding
int itemPadding = textView.getCompoundPaddingLeft() + textView.getPaddingRight();
//通过画笔设置字体大小,便于后面测量item的宽度的准确性。但是他没有实际设置textview的大小,只是用于测量
Paint paint = new Paint();
paint.setTextSize(textView.getTextSize());
//设置每行的间距
LinearLayout.LayoutParams lineParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lineParam.setMargins(0, lineMargin, 0, 0);
//设置没列的间距
LinearLayout.LayoutParams collwParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
collwParam.setMargins(0, 0, itemMargin, 0);
//设置行,可以在layout添加条目
LinearLayout layout = new LinearLayout(context);
layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.HORIZONTAL);
container.addView(layout);
int remainWidth = containerWidth;
for (int i = 0; i < list.size(); i++) {
String text = list.get(i);
float itemWidth = paint.measureText(text) + itemPadding;
if (remainWidth > itemWidth) {
addTextView(text, layout, collwParam);
} else {
layout = new LinearLayout(context);
layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(lineParam);
addTextView(text, layout, collwParam);
container.addView(layout);
remainWidth = containerWidth;
}
remainWidth = (int) (remainWidth - itemWidth + 0.5f) - itemMargin;
}
resetTextViewMarginsRight(layout);
}
//每一行添加的textView
public void addTextView(String text, ViewGroup layout, ViewGroup.LayoutParams itemParams) {
TextView tv = (TextView) LayoutInflater.from(context).inflate(resourceId, null);
tv.setText(text);
layout.addView(tv, itemParams);
}
//将每行最后一个textview的MarginsRight去掉
private void resetTextViewMarginsRight(ViewGroup viewGroup) {
TextView textView = (TextView) viewGroup.getChildAt(viewGroup.getChildCount() - 1);
textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
}
这就是如何使用此工具的。
ublic class MainActivity extends AppCompatActivity {
ViewGroup container;
private boolean isFristTime = true;
/** 标签之间的间距 px */
final int itemMargins = 50;
/** 标签的行间距 px */
final int lineMargins = 50;
List stringList=new ArrayList<>();
private String[] tags = { "大约在冬季", "漂洋过海的来看你", "天下有情人", "我很认真", "夜夜夜夜", "想你的夜", "背叛", "趁早", "旧情绵绵", "谁明浪子心", "安妮",
"说谎的爱人", "不浪漫的罪名", "不愿一个人", "风吹麦浪" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container= (ViewGroup) findViewById(R.id.container);
Log.e("width", "onCreate: "+container.getMeasuredWidth());
for (int i=0;i<10;i++){
stringList.add("大在冬季"+i);
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && isFristTime){
isFristTime = false;
// init();
LinearAddUtil linearUtil=new LinearAddUtil(R.layout.item_textview,container,stringList,this,50,50);
}
}
}
注意:
在Android中,有时我们需要测量控件的宽度和高度进行一些运算,特别是在自适应屏幕的时候,这些计算就变得特别必要,但是,如果我们直接在onCreate,
或者onStart、onResume(第一次执行时)方法,去获取控件的宽度和高度时,得出的结果会是0.
原因是:在执行这几个方法时,窗口Window对象并没有创建完成,而只是做了一些初始化的操作,想要获取控件的宽度和高度就要等到onDraw方法执行完成之后。