我正在尝试动态生成文本视图。我创建它,但是。输出在上下文的开头给出了第一个字母,而在上下文的结尾则给出了其他排序。
ArrayList<String> letters = new ArrayList<String>();
letters.add("Y");
letters.add("Ü");
letters.add("Z");
letters.add("S");
letters.add("Ü");
letters.add("Z");
LinearLayout llTv = findViewById(R.id.llTv);
TextView tv;
for (int j = 0; j<sQuestion.length(); j++)
{
View main_view = new View(MainActivity.this);
main_view.setLayoutParams(new LinearLayout.LayoutParams(70,
LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams lP = new LinearLayout.LayoutParams(main_view.getLayoutParams());
lP.setMargins(10 ,10,10,10);
llTv.setGravity(Gravity.CENTER_HORIZONTAL);
tv = new TextView(MainActivity.this);
tv.setText(letters.get(j));
tv.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
tv.setPadding(15, 15, 15, 15);
tv.setTextColor(getResources().getColor(R.color.colorPrimaryLight));
tv.setLinksClickable(true);
tv.setTextSize(25);
llTv.addView(tv,lP);
llTv.addView(main_view,1);
}
答案 0 :(得分:0)
与文本视图一起使用
tv.setLayoutParams(new LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT,android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));