作者:忧郁王囝 | 来源:互联网 | 2023-05-17 03:16
1,先在XML里定义控件LinearLayout
android:id="@+id/layout_yewufenbuview"
android:layout_
android:layout_
android:layout_marginBottom="15dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:orientation="vertical" >
2,动态添加TextView方法
/**
*
* @Description 设置策略的信息
* @author helen.yang
*/
private void setTextViewYeWuInfos(List companyBusinessProfiles, LinearLayout layout) {
try {
// LinearLayout layout = (LinearLayout)
// findViewById(R.id.strategy_listinfos_ll);
int screenWidth = Tools.getWidth(this);
int leftcount = 2 * Tools.dip2px(mcontext, 20);
int measurewidth = screenWidth - leftcount;
final LayoutInflater inflater = LayoutInflater.from(this);
/** 用来测量字符的宽度 */
final Paint paint = new Paint();
final TextView textView = (TextView) inflater.inflate(R.layout.industry_detailyewufenbuitem, null);
final int itemPadding = textView.getCompoundPaddingLeft() + textView.getCompoundPaddingRight();
final LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tvParams.setMargins(0, 0, Tools.dip2px(mcontext, 10), 0);
paint.setTextSize(textView.getTextSize());
LinearLayout child = new LinearLayout(this);
child.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
child.setOrientation(LinearLayout.HORIZONTAL);
layout.removeAllViews();
layout.addView(child);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, Tools.dip2px(mcontext, 10), 0, 0);
/** 一行剩下的空间 **/
int remainWidth = measurewidth;
if (companyBusinessProfiles != null && companyBusinessProfiles.size() > 0) {
for (int i = 0; i StringBuilder stringBuilder = new StringBuilder(companyBusinessProfiles.get(i).getSzh());
stringBuilder.append(" ");
double ratio = Tools.mul(companyBusinessProfiles.get(i).getRatio(), 100);
stringBuilder.append((int) Tools.round(ratio, 0));
stringBuilder.append("%");
String showString = stringBuilder.toString();
final String text = showString;
final float itemWidth = paint.measureText(text) + itemPadding;
if (remainWidth > itemWidth) {
addItemView(inflater, child, tvParams, text, R.layout.industry_detailyewufenbuitem, companyBusinessProfiles.get(i).getCode(), companyBusinessProfiles.get(i).getSzh());
} else {
resetTextViewMarginsRight(child);
child = new LinearLayout(this);
child.setLayoutParams(params);
child.setOrientation(LinearLayout.HORIZONTAL);
/** 将前面那一个textview加入新的一行 */
addItemView(inflater, child, tvParams, text, R.layout.industry_detailyewufenbuitem, companyBusinessProfiles.get(i).getCode(), companyBusinessProfiles.get(i).getSzh());
layout.addView(child);
remainWidth = measurewidth;
}
remainWidth = (int) (remainWidth - itemWidth - Tools.dip2px(mcontext, 1)) - Tools.dip2px(mcontext, 10);
if (remainWidth <0) {
remainWidth = 0;
}
}
resetTextViewMarginsRight(child);
}
} catch (Exception e) {
// TODO: handle exception
}
}
/***************** 将每行最后一个textview的MarginsRight去掉 *********************************/
private void resetTextViewMarginsRight(ViewGroup viewGroup) {
final TextView tempTextView = (TextView) viewGroup.getChildAt(viewGroup.getChildCount() - 1);
tempTextView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
private void addItemView(LayoutInflater inflater, ViewGroup viewGroup, LayoutParams params, String text, int id) {
final TextView tvItem = (TextView) inflater.inflate(id, null);
tvItem.setText(text);
viewGroup.addView(tvItem, params);
}