热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

LinearLayout动态添加TextView控件

1,先在XML里定义控件LinearLayout<LinearLayoutandroid:id"@+idlayout_yewufe

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);
}



推荐阅读
author-avatar
忧郁王囝
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有