Android的设置重力一个TextView编程重力、Android、TextView

2023-09-12 08:28:36 作者:伤半世流离

在一个TextView得到我想要的结果|center_horizo​​ntal底在XML中,但我需要以编程方式做到这一点比重=:

我可以使用机器人。我的TextView是在 tablerow的如果在重要的一个 RelativeLayout的

我曾尝试:

 的LayoutParams的LayoutParams =新TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
labelTV.setLayoutParams(的LayoutParams);
 

但是,如果我理解正确的话,将其应用到 tablerow的,而不是TextView中?

解决方案

  labelTV.setGravity(Gravity.CENTER | Gravity.BOTTOM);
 

此外,你在说什么的重力的或约layout_gravity?后者将不会在上班的 RelativeLayout的的。

Android编程课程1TextView国际化

I can use android:gravity="bottom|center_horizontal" in xml on a textview to get my desired results, but I need to do this programmatically. My textview is inside a tablerow if that matters in a relativelayout.

I have tried:

LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
labelTV.setLayoutParams(layoutParams);

But if I understand correctly, that would apply it to the tablerow, not the textview?

解决方案

labelTV.setGravity(Gravity.CENTER | Gravity.BOTTOM);

Also, are you talking about gravity or about layout_gravity? The latter won't work in a RelativeLayout.