以编程方式居中 TextView 文本

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

我试图通过代码水平和垂直地居中一个 TextView项目(我可以用 XML 完成)。我已经看到了一些当父对象是一个表或其他对象时如何实现这一点的示例,但是我希望这对我来说更容易掌握。(附注: 请随意更正我的术语)。

下面是来自教程/我的工作模型的示例代码:

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

我已经设法找到了 setGravity方法,并试图涉足 setLayoutParams,但我不确定它的作用域是什么,因为我无法找到我应该导入什么来解析 WRAP_CONTENT常量。据我所知,居中和内容包装 + 重力是两个不同的概念。我想举一个例子,说明如何在这种情况下同时进行这两项工作,以及如何/在哪里可以在 API 文档中找到答案?

117610 次浏览

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

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

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);
textView.setLayoutParams(params);
textView.setGravity(Gravity.CENTER);


layout.addView(textView);


setcontentView(layout);
yourTextView.setGravity(Gravity.CENTER);

For dynamically center

textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
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

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 :

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

You can use the following to programmatically center TextView text in Kotlin:

textview.gravity = Gravity.CENTER

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

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)