我需要动态创建texview,它仅适用于文字内容展示VERTICAL
,但HORIZONTAL
TextView tv = new TextView(this.theContext);
tv.setText(this.getTip(0));
tv.setBackgroundColor(Color.BLUE);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);
params.gravity = Gravity.CENTER;
this.addView(tv, params);
mLeftLabel = tv;
另外,我设置了布局:
tv.layout(l,t,r,b);
。但我认为这句话不应影响文字方向,是不是?
请给予帮助或提示,谢谢。
答案 0 :(得分:1)
尝试按照以下示例:
TextView tv = new TextView(this);
tv.setText("text");
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);
params.gravity = Gravity.CENTER;
mainLayout.addView(tv, params);
答案 1 :(得分:0)
尝试将LayoutParams更改为
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
当您使用WRAP_CONTENT进行textview时,将重力设置为中心似乎没有意义,但我不确定。