在 Android 的 Toast 中居中文本

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

Center text in a toast in Android

androidtextalignmentcentertoast

提问by Sonoman

I was wondering if there was a way to display all text in a toast to be centered. For instance, I have a toast that has 2 lines of text in it. For purely aesthetic reasons, I would like the text to center-aligned instead of left-aligned. I've looked through the documentation and can't find anything about it. Is there a simple way to do this that I have missed?

我想知道是否有办法在吐司中显示所有文本以使其居中。例如,我有一个吐司,里面有 2 行文本。纯粹出于审美原因,我希望文本居中对齐而不是左对齐。我已经浏览了文档,但找不到任何关于它的信息。有没有一种简单的方法可以做到这一点,我错过了?

Thanks Chris

谢谢克里斯

采纳答案by Sameer Segal

Use the Toast's setView(view)function to supply a Viewwith Gravity.CENTER.

使用 Toast 的setView(view)函数提供一个Viewwith Gravity.CENTER

回答by Marc

Adapted from another answer:

改编自另一个答案

Toast toast = Toast.makeText(this, "Centered\nmessage", Toast.LENGTH_SHORT);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if( v != null) v.setGravity(Gravity.CENTER);
toast.show();

回答by Sephy

Toast is built on a TextView and the default gravity of it is left aligned. So, you need to create your own TextView like this for instance :

Toast 建立在 TextView 之上,它的默认重力是左对齐的。因此,您需要像这样创建自己的 TextView,例如:

<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical|center_horizontal"
    android:text="all the text you want"
/>

And you assign the TextView to the Toast like this :

然后将 TextView 分配给 Toast,如下所示:

Toast t = new Toast(yourContext);
t.setView(yourNewTextView);

回答by Mikhail

It is dirty hack, but

这是肮脏的黑客,但是

((TextView)((LinearLayout)toast.getView()).getChildAt(0))
    .setGravity(Gravity.CENTER_HORIZONTAL);

回答by YetAnotherUser

Without the hacks:

没有黑客:

String text = "Some text";
Spannable centeredText = new SpannableString(text);
centeredText.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
            0, text.length() - 1,
            Spannable.SPAN_INCLUSIVE_INCLUSIVE);

Toast.makeText(getActivity(), centeredText, Toast.LENGTH_LONG).show();

There are also another alignments besides center.

除了中心之外,还有另一种对齐方式。

source

来源

回答by serj

Toast toast = Toast.makeText(this, "Message", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

回答by imbryk

Not saing that findViewById(android.R.id.message)is wrong, but just in case there are (future?) implementation differences I myself used a bit differen approach:

不是说这findViewById(android.R.id.message)是错误的,但以防万一(未来?)实现差异,我自己使用了一些不同的方法:

void centerText(View view) {
    if( view instanceof TextView){
        ((TextView) view).setGravity(Gravity.CENTER);
    }else if( view instanceof ViewGroup){
        ViewGroup group = (ViewGroup) view;
        int n = group.getChildCount();
        for( int i = 0; i<n; i++ ){
            centerText(group.getChildAt(i));
        }
    }
}

and then:

进而:

Toast t = Toast.makeText(context, msg,Toast.LENGTH_SHORT);
centerText(t.getView());
t.show();

回答by Kumar VL

It is work for me:

这对我来说是工作:

Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER| Gravity.BOTTOM, 0, 20);
toast.show();

回答by vishnu benny

In kotlin:

科特林

fun makeToast(context: Context, resId: Int) {
    val toast = Toast.makeText(context, resId, Toast.LENGTH_SHORT)
    val view = toast.view.findViewById<TextView>(android.R.id.message)
    view?.let {
        view.gravity = Gravity.CENTER
    }
    toast.show()
}

回答by Muhamed Huseinba?i?

This variation is with using LinearLayout. :)

这种变化与使用 LinearLayout 有关。:)

Toast SampleToast = Toast.makeText(this, "This is the example of centered text.\nIt is multiline text.", Toast.LENGTH_SHORT);
LinearLayout OurLayout = (LinearLayout) SampleToast.getView();

if (OurLayout.getChildCount() > 0) 
{
TextView SampleView = (TextView) OurLayout.getChildAt(0);
SampleView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
}

SampleToast.show();