我已经动态创建了一个文本视图。我想将它的重力设置为右。我使用了textview.setGravity(Gravity.RIGHT),但它不起作用。当我使用它时,文本视图只对齐左方向。如何设置textview的重力?我不想使用setPadding()。
textMore[i]=new TextView(this);
textMore[i].setGravity(Gravity.Right);
textMore[i].setText("more....");
textMore[i] .setTextColor(Color.BLUE);
答案 0 :(得分:0)
您无法通过setGravity
更改TextView的对齐方式,它会更改TextView内容对齐方式。
答案 1 :(得分:0)
你可以做到
LinearLayout.LayoutParams textParams = (LinearLayout.LayoutParams) textMore[i].getLayoutParams();
textParams.gravity = Gravity.RIGHT;
textMore[i].setLayoutParams(textParams);
然后在动态LinearLayout
答案 2 :(得分:0)
如果您的文字视图的父级是RelativeLayout (更喜欢这个)
set:alignParentRight
如果文字视图的父级是LinearLayout
set:setLayoutGravity(Gravity.Right)
或
LinearLayout.LayoutParams textParams = (LinearLayout.LayoutParams)textMore[i].getLayoutParams();
textParams.gravity = Gravity.RIGHT;
textMore[i].setLayoutParams(textParams);
setGravity(Gravity.Right) ==>此属性将对齐textView中的文本,而不是将视图与其父对齐。
答案 3 :(得分:0)
首先将textView Width属性设置为填充父级,然后将其设置为右侧 如果你使用线性布局