java - 如何修复 TextView 太大?
问题描述
我正在开发一个带有sqlite
数据库的项目,但我似乎无法解决这个问题。我该如何解决?这是我的代码:
TableRow.LayoutParams trs= new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
trs.setMargins(0,0,0,0);
for (int i = 0; i < id.length; i++)
{
/** Create a TableRow dynamically **/
tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/** Creating a TextView to add to the row **/
idtxt = new TextView(this);
AutofitHelper.create(idtxt);
idtxt.setBackgroundColor(Color.parseColor("#00ff00"));
idtxt.setText(id[i]);
idtxt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
if(idtxt.getLineCount() > 1){
idtxt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
}
}
});
idtxt.setTextColor(Color.RED);
idtxt.setWidth(-100);
idtxt.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
idtxt.setLayoutParams(trs);
// idtxt.setPadding(0, 0, 0, 5);
idtxt.setBackgroundColor(new Color(Color.green()));
tr.addView(idtxt); // Adding textView to tablerow.
tl.addView(tr, new TableLayout.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
// Add the TableRow to the TableLayout
/** Creating a TextView to add to the row **/
timestamptxt = new TextView(this);
timestamptxt.setText(Timestamp[i]);
timestamptxt.setTextColor(Color.RED);
timestamptxt.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
timestamptxt.setLayoutParams(trs);
timestamptxt.setPadding(-5, -5, -5, -5);
tr.addView(timestamptxt);
tl.removeView(tr);
tl.addView(tr, new TableLayout.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/** Creating a TextView to add to the row **/
cosplaytxt = new TextView(this);
cosplaytxt.setText(Cosplay_Name[i]);
cosplaytxt.setTextColor(Color.RED);
cosplaytxt.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
cosplaytxt.setLayoutParams(trs);
cosplaytxt.setPadding(5, 5, 5, 5);
tr.addView(cosplaytxt);
tl.removeView(tr);
tl.addView(tr, trs);
解决方案
请将 textview 的高度和宽度设置为 wrap_content。目前 textview 的宽度是 match_parent。尝试这个。
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
推荐阅读
- spring-boot - Hibernate - Spring - ConstraintViolationException - UniqueConstraint
- mysql - 如何通过 Shell 在 MySQL 中升级?
- c - 分配了 strcat char* 的分段错误
- vim - init.lua 中的 Autocmd InsertEnter 到高亮光标不起作用
- java - rawPassword 不能为空异常
- java - SendZuulResponse 方法用法
- ffmpeg - 为什么我只使用 libavformat 的 av_read_frame 部分读取 mp3 文件?
- javascript - 如何使所有元素正确地与 onmouseover、onmouseout 一起使用?
- javascript - 与浏览器 Javascript 的事件侦听器相比,事件包在 node.js 中如何工作
- spring-boot - Spring-boot:Spring-boot Web 应用程序(Rest API)WAR 文件未运行