Android 以编程方式居中 TextView 文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12775743/
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:03:24  来源:igfitidea点击:

Programmatically center TextView text

androidtextview

提问by Alex Summers

I beg some leniency here, I'm just starting with the Android SDK tutorials and I'm attempting something out of interest that's not in the tutorial itself, but I would hope would be easy.

我在这里请求宽大处理,我只是从 Android SDK 教程开始,我正在尝试一些不属于教程本身的兴趣,但我希望会很容易。

I am trying to center a TextViewitem via code horizontally and vertically (I can do it in XML just fine). I've seen several examples of how to do this when the parent is a table or some other object, but I hope this would be easier for me to grasp. (p.s. Feel free to correct my terminology).

我试图TextView通过代码水平和垂直居中(我可以在 XML 中做到这一点)。我已经看到了几个示例,说明当父对象是桌子或其他对象时如何执行此操作,但我希望这对我来说更容易掌握。(ps 请随意更正我的术语)。

Here is the example code from the tutorial / my working model:

这是教程/我的工作模型中的示例代码:

package com.example.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;


public class DisplayMessageActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);
        textView.setGravity(View.TEXT_ALIGNMENT_GRAVITY);

        setContentView(textView);
    }
}

I've managed to locate the setGravitymethod, and I've tried to dabble in the setLayoutParamsfor it, but I'm not sure what the scope is for it as I can't locate what I should be importing to get the WRAP_CONTENTconstant to resolve. From what I understood, centering and content_wrapping+gravity are two separate things. I'd like an example of how to do both in this case and maybe how/where I would have found the answer in the API documentation?

我设法找到了该setGravity方法,并尝试涉足setLayoutParams它,但我不确定它的范围是什么,因为我无法找到应该导入的内容WRAP_CONTENT来解决常量. 据我了解,居中和 content_wrapping+gravity 是两个独立的事情。我想要一个在这种情况下如何做的例子,也许我会在 API 文档中找到答案的方式/位置?

回答by Syn3sthete

yourTextView.setGravity(Gravity.CENTER);

回答by Nirav Ranpara

For dynamically center

对于动态居中

textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

回答by Naveen Tamrakar

TextView text = new TextView(this);

text.setGravity(Gravity.CENTER);

and

text.setGravity(Gravity.TOP);

and

text.setGravity(Gravity.BOTTOM);

and

text.setGravity(Gravity.LEFT);

and

text.setGravity(Gravity.RIGHT);

and

text.setGravity(Gravity.CENTER_VERTICAL);

and

text.setGravity(Gravity.CENTER_HORIZONTAL);

And More Also Avaliable

还有更多可用

回答by Kalpesh Lakhani

this will work for sure..

这肯定会起作用..

RelativeLayout layout = new RelativeLayout(R.layout.your_layour); 
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(LinearLayout.CENTER_IN_PARENT);
textView.setLayoutParams(params);
textView.setGravity(Gravity.CENTER);

layout.addView(textView);

setcontentView(layout);

回答by Antrromet

Try adding the following code for applying the layout params to the TextView

尝试添加以下代码以将布局参数应用于 TextView

LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(LinearLayout.CENTER_IN_PARENT);
textView.setLayoutParams(lp);

回答by arif ardiansyah

if your text size is small, you should make the width of your text view to be "fill_parent". After that, you can set your TextView Gravity to center :

如果您的文本尺寸较小,您应该将文本视图的宽度设置为“fill_parent”。之后,您可以将 TextView Gravity 设置为 center :

TextView textView = new TextView(this);
textView.setText(message);
textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER);

回答by Ads

You can use the following to programmatically center TextViewtext in Kotlin:

您可以使用以下内容以编程方式TextView在 Kotlin 中居中文本:

textview.gravity = Gravity.CENTER

回答by Sai Gopi N

try this method

试试这个方法

  public void centerTextView(LinearLayout linearLayout) {
    TextView textView = new TextView(context);
    textView.setText(context.getString(R.string.no_records));
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(18.0f);
    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    linearLayout.addView(textView);
}

回答by kioa845 - The Samoan Developer

These two need to go together for it to work. Been scratching my head for a while.

这两个需要一起工作才能发挥作用。挠我的头有一段时间了。

numberView.textAlignment = View.TEXT_ALIGNMENT_CENTER
 numberView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)