68

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

4

11 回答 11

111

改编自另一个答案

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();
于 2012-11-21T12:01:21.803 回答
28

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"
/>

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

Toast t = new Toast(yourContext);
t.setView(yourNewTextView);
于 2010-08-19T13:28:19.190 回答
25

没有黑客:

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

除了中心之外,还有其他对齐方式。

参看。TextView中的多重对齐?

于 2015-01-15T21:16:27.693 回答
19

这是肮脏的黑客,但是

((TextView)((LinearLayout)toast.getView()).getChildAt(0))
    .setGravity(Gravity.CENTER_HORIZONTAL);
于 2011-05-01T08:35:02.957 回答
15

使用 Toast 的setView(view)功能来提供一个Viewwith Gravity.CENTER

于 2010-08-19T13:20:11.493 回答
13
Toast toast = Toast.makeText(this, "Message", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
于 2011-05-07T15:10:24.453 回答
5

并不是说这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));
        }
    }
}

接着:

Toast t = Toast.makeText(context, msg,Toast.LENGTH_SHORT);
centerText(t.getView());
t.show();
于 2014-04-24T10:18:39.717 回答
2

科特林

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()
}
于 2018-06-06T06:20:09.853 回答
1

It is work for me:

Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER| Gravity.BOTTOM, 0, 20);
toast.show();
于 2018-03-13T06:53:29.837 回答
0

这种变化是LinearLayout在您的布局 xml 中使用的 :)

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();
于 2015-02-02T10:41:46.550 回答
-3
Toast t=Toast.makeText(getApplicationContext(),"Text",Toast.LENGTH_LONG);
t.setText("Password Does't match...");
t.setGravity(0, 0, 0);
t.show();

吐司的简单代码最重要

于 2016-02-03T19:37:51.053 回答