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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3775705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 12:06:10  来源:igfitidea点击:

How set the android:gravity to TextView from Java side in Android

androidandroid-layouttextviewgravity

提问by Nate

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 tablerowif that matters in a relativelayout.

我可以android:gravity="bottom|center_horizontal"在 textview 上的 xml中使用以获得我想要的结果,但我需要以编程方式执行此操作。我的TextView是内tablerow,如果在该事项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?

但如果我理解正确,那会将它应用于tablerow,而不是文本视图?

回答by Maragues

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

Kotlin version (thanks to Thommy)

Kotlin 版本(感谢 Thommy)

labelTV.gravity = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM

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

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

回答by radiofrequency

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);

回答by Bajrang Hudda

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

这相当于下面的xml代码

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

回答by Irshu

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

你应该使用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.

不会工作。这将为小部件设置重力,而不是为它的文本设置。

回答by Faxriddin Abdullayev

Use this code

使用此代码

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

回答by DAS

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

This will set gravity of your textview.

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

回答by Roman Fernandez

Solved this by doing a few things, first getting the heightof my TextViewand diving it by the text sizeto get the total amount of lines possible with the TextView.

通过做一些事情解决了这个问题,首先获取heightmy 的TextView并通过文本深入size了解TextView.

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

After you get this value you need to set your TextViewmaxLinesto this new value.

获得此值后,您需要将其设置TextViewmaxLines为此新值。

TextView.setMaxLines(maxLines);

Set the Gravityto Bottomonce the maximum amount of lines has been exceeded and it will scroll down automatically.

设置GravityBottom一次线的最高金额已经超过,它会自动向下滚动。

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.

为了这个正常工作,你必须使用append()TextView,如果setText()这是不行的。

TextView.append("Your Text");

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

这种方法的好处是,无论height您的TextView和文本是什么,都可以动态使用size。如果您决定对布局进行修改,此代码仍然有效。