gpt4 book ai didi

具有多种颜色的 Android Toast

转载 作者:太空狗 更新时间:2023-10-29 15:38:46 26 4
gpt4 key购买 nike

我想要一个有多种颜色的 toast 。像这样:

Multi Color Toast

我查看了有关更改 xml 中的布局以创建自定义 Toast 的不同教程,但没有一个教程解释如何像这样添加不同的颜色。

我该怎么做?

================================

回答

================================

在您的帮助下,我设计了一个简单的 Method() 来使彩色 toast 更容易调用。

res/layout/toast_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingBottom="6dp"
android:paddingTop="6dp"
android:background="#DAAA"
>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

src/PROJECTNAME/FILENAME.java

// Color Toast(String1,String2,Color)
// Toastbackground = White
// String1 = Dark Gray
// String2 - **CASE SENSITIVE**
// = "same" = Dark Gray, or
// = "purple" = Purple, or
// = "orange" = Orange

public void CToast(String t1, String t2, String c) {
if (c == "same") {
c = "444444";
} else if (c == "purple") {
c = "6600FF";
} else if (c == "orange") {
c = "ffcc00";
}

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView textCToast = (TextView) layout.findViewById(R.id.text);

String text2 = "<font color=#444444>" + t1 + "</font> <font color=#" + c + ">" + t2 + "</font";
textCToast.setText(Html.fromHtml(text2));

Toast toast = new Toast(this);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
}

希望对您有所帮助!谢谢大家

最佳答案

在您的自定义布局中放置一个 textView。有

String text = "This is <font color='red'>red</font>. This is <font color='blue'>blue</font>.";
textView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);

N.B.. 示例取自 here

关于具有多种颜色的 Android Toast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16036604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com