如何在 Android 中从 Java 端将 android:gravity 设置为 TextView

发布于 2024-09-24 13:41:43 字数 474 浏览 6 评论 0原文

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

我已经尝试过:

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

但是如果我理解正确的话,那会将其应用于tablerow,而不是textview?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

超可爱的懒熊 2024-10-01 13:41:43
labelTV.setGravity(Gravity.CENTER | Gravity.BOTTOM);

Kotlin 版本(感谢 Thommy)

labelTV.gravity = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM

另外,您是在谈论重力还是layout_gravity?后者在RelativeLayout中不起作用。

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

Kotlin version (thanks to Thommy)

labelTV.gravity = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM

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

枉心 2024-10-01 13:41:43

这将使文本在文本视图中居中:

TextView ta = (TextView) findViewById(R.layout.text_view);
LayoutParams lp = new LayoutParams();
lp.gravity = Gravity.CENTER_HORIZONTAL;
ta.setLayoutParams(lp);

This will center the text in a text view:

TextView ta = (TextView) findViewById(R.layout.text_view);
LayoutParams lp = new LayoutParams();
lp.gravity = Gravity.CENTER_HORIZONTAL;
ta.setLayoutParams(lp);
时光暖心i 2024-10-01 13:41:43

我们可以在任何视图上设置布局重力,如下所示 -

myView = findViewById(R.id.myView);
myView.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);
 or
myView.setGravity(Gravity.BOTTOM);

这相当于下面的 xml 代码

<...
 android:gravity="center_vertical|right"
 ...
 .../>

We can set layout gravity on any view like below way-

myView = findViewById(R.id.myView);
myView.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);
 or
myView.setGravity(Gravity.BOTTOM);

This is equilent to below xml code

<...
 android:gravity="center_vertical|right"
 ...
 .../>
櫻之舞 2024-10-01 13:41:43

您应该使用textView.setGravity(Gravity.CENTER_HORIZONTAL);。

请记住,使用

LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams2.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;

不会起作用。这将为小部件而不是其文本设置重力。

You should use textView.setGravity(Gravity.CENTER_HORIZONTAL);.

Remember that using

LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams2.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;

won't work. This will set the gravity for the widget and not for it's text.

红尘作伴 2024-10-01 13:41:43

使用此代码

        TextView textView = new TextView(YourActivity.this);
        textView.setGravity(Gravity.CENTER | Gravity.TOP);
        textView.setText("some text");

Use this code

        TextView textView = new TextView(YourActivity.this);
        textView.setGravity(Gravity.CENTER | Gravity.TOP);
        textView.setText("some text");
猫卆 2024-10-01 13:41:43
textView.setGravity(Gravity.CENTER | Gravity.BOTTOM);

这将设置文本视图的重力。

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

This will set gravity of your textview.

乖乖公主 2024-10-01 13:41:43

通过做一些事情解决了这个问题,首先获取我的 TextViewheight 并除以文本 size 以获得总行数可以使用TextView

int maxLines = (int) TextView.getHeight() / (int) TextView.getTextSize();

获得此值后,您需要将 TextView maxLines 设置为这个新值。

TextView.setMaxLines(maxLines);

一旦超过最大行数,将Gravity设置为Bottom,它将自动向下滚动。

if (TextView.getLineCount() >= maxLines) {
    TextView.setGravity(Gravity.BOTTOM);
}

为了使其正常工作,您必须对 TextView 使用 append(),如果您 setText() 这将不起作用。

TextView.append("Your Text");

此方法的好处是,无论 TextViewheight 和文本 size 是多少,都可以动态使用它。如果您决定修改布局,此代码仍然有效。

Solved this by doing a few things, first getting the height of my TextView and diving it by the text size to get the total amount of lines possible with the TextView.

int maxLines = (int) TextView.getHeight() / (int) TextView.getTextSize();

After you get this value you need to set your TextView maxLines to this new value.

TextView.setMaxLines(maxLines);

Set the Gravity to Bottom once the maximum amount of lines has been exceeded and it will scroll down automatically.

if (TextView.getLineCount() >= maxLines) {
    TextView.setGravity(Gravity.BOTTOM);
}

In order for this to work correctly, you must use append() to the TextView, If you setText() this will not work.

TextView.append("Your Text");

The benefit of this method is that this can be used dynamically regardless of the height of your TextView and the text size. If you decide to make modifications to your layout this code would still work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文