如何在 Android 中从 Java 端将 android:gravity 设置为 TextView [英] How set the android:gravity to TextView from Java side in Android

查看:35
本文介绍了如何在 Android 中从 Java 端将 android:gravity 设置为 TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 textview 上的 xml 中使用 android:gravity="bottom|center_horizo​​ntal" 来获得我想要的结果,但我需要以编程方式执行此操作.如果在 relativelayout 中很重要,我的 textview 位于 tablerow 内.

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.

我试过了:

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

但是如果我理解正确的话,这会将它应用到 tablerow,而不是 textview?

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

推荐答案

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

Kotlin 版本(感谢 Thommy)

Kotlin version (thanks to Thommy)

labelTV.gravity = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM

此外,您是在谈论 gravity 还是 layout_gravity?后者在 RelativeLayout 中不起作用.

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

这篇关于如何在 Android 中从 Java 端将 android:gravity 设置为 TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆