【问题标题】:Android Set the text align to the middle of the toast [duplicate]Android设置文本对齐到吐司的中间[重复]
【发布时间】:2012-02-28 20:54:37
【问题描述】:

我有一条很长的祝酒词。我想将文本设置在中间而不是开始向左对齐。

这可能吗?

【问题讨论】:

    标签: android android-view


    【解决方案1】:

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

    Source

    【讨论】:

    • 谢谢 Raoul,我会试试这个。
    【解决方案2】:

    这里:

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

    【讨论】:

    • 谢谢@dmytrodanylyk,但我想将吐司内的文本设置在中间,而不是所有吐司。
    猜你喜欢
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 2018-09-13
    相关资源
    最近更新 更多