Android 我喜欢烤面包
我有自己的看法。我想在android中像祝酒一样展示customviewAndroid 我喜欢烤面包,android,android-custom-view,Android,Android Custom View,我有自己的看法。我想在android中像祝酒一样展示customview Toast.makeText(getApplicationContext(),"text",Toast.LENGTH_LONG).show() 也就是说,我希望我的自定义视图通过更改文本等基本属性显示在应用程序中的任何位置(应用程序中的全局) 请给我指一下正确的方向 请注意:我不想使用addView()和removeView()来添加和删除自定义视图。自定义Toast在我的情况下也不起作用,因为我需要使用此customv
Toast.makeText(getApplicationContext(),"text",Toast.LENGTH_LONG).show()
也就是说,我希望我的自定义视图通过更改文本等基本属性显示在应用程序中的任何位置(应用程序中的全局)
请给我指一下正确的方向
请注意:我不想使用addView()和removeView()来添加和删除自定义视图。自定义Toast在我的情况下也不起作用,因为我需要使用此customview。您可以拥有自定义Toast,方法将获取文本并显示。请查看
public void showToast(String msg) {
LayoutInflater li = getLayoutInflater();
View layout = li.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_layout));
TextView toastmsg = (TextView) layout.findViewById(R.id.custom_toast_message);
toastmsg.setText(msg);
Toast toast = new Toast(this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(layout);
toast.show();
}
自定义_toast_message.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="@+id/custom_toast_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/toast_bg"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/custom_toast_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Custom Toast"
android:padding="15dp"
android:src="@drawable/icon_toast_alert" />
<TextView
android:id="@+id/custom_toast_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Custom Toast"
android:padding="15dp"
android:text="Custom Toast"
android:textAlignment="center"
android:textColor="@color/colorWhite"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
自定义土司如下所示:
快乐编码 您可以自定义toast,方法将获取文本并显示。请查看
public void showToast(String msg) {
LayoutInflater li = getLayoutInflater();
View layout = li.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_layout));
TextView toastmsg = (TextView) layout.findViewById(R.id.custom_toast_message);
toastmsg.setText(msg);
Toast toast = new Toast(this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(layout);
toast.show();
}
自定义_toast_message.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="@+id/custom_toast_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/toast_bg"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/custom_toast_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Custom Toast"
android:padding="15dp"
android:src="@drawable/icon_toast_alert" />
<TextView
android:id="@+id/custom_toast_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Custom Toast"
android:padding="15dp"
android:text="Custom Toast"
android:textAlignment="center"
android:textColor="@color/colorWhite"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
自定义土司如下所示:
快乐编码 扩展Toast类非常简单 假设我的自定义Toast Cals是NexoolCustomToast,它扩展了Toast类。下面是自定义Toast类的代码 在本课程中,我使用的是nexool_fail_custom_toast.xml、nexool_success_custom_toast.xml中的xml布局,具有四种布局屏幕大小(大布局、普通布局、小布局、大布局) 对于红色故障布局
扩展Toast类真的很容易 假设我的自定义Toast Cals是NexoolCustomToast,它扩展了Toast类。下面是自定义Toast类的代码 在本课程中,我使用的是nexool_fail_custom_toast.xml、nexool_success_custom_toast.xml中的xml布局,具有四种布局屏幕大小(大布局、普通布局、小布局、大布局) 对于红色故障布局
在common.java文件中创建公共方法
public static void displayCustomToast(Activity activity, String message, String length) {
// if you want to set typeface for toast text then use this //
Typeface tfShruti = Typeface.createFromAsset(activity.getAssets(), "fonts/shruti.ttf");
LayoutInflater inflater = activity.getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_layout,
(ViewGroup) activity.findViewById(R.id.custom_toast_layout_id));
// set a message
TextView text = (TextView) layout.findViewById(R.id.tv_toast);
text.setText(message);
text.setTypeface(tfShruti);
// Toast...
Toast toast = new Toast(activity.getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, 0);
//toast.setMargin(0,10);
//toast.setGravity(Gravity.TOP | Gravity.LEFT, 40, 60);
//toast.setDuration(Toast.LENGTH_LONG);
if (length.equalsIgnoreCase("short")) {
toast.setDuration(Toast.LENGTH_SHORT);
} else {
toast.setDuration(Toast.LENGTH_LONG);
}
toast.setView(layout);
toast.show();
}
创建这样的布局
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_layout_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/rectangle_fill_black_color"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/tv_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textColor="#ffffff"
android:textSize="17sp" />
</LinearLayout>
或
在common.java文件中创建公共方法
public static void displayCustomToast(Activity activity, String message, String length) {
// if you want to set typeface for toast text then use this //
Typeface tfShruti = Typeface.createFromAsset(activity.getAssets(), "fonts/shruti.ttf");
LayoutInflater inflater = activity.getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_layout,
(ViewGroup) activity.findViewById(R.id.custom_toast_layout_id));
// set a message
TextView text = (TextView) layout.findViewById(R.id.tv_toast);
text.setText(message);
text.setTypeface(tfShruti);
// Toast...
Toast toast = new Toast(activity.getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, 0);
//toast.setMargin(0,10);
//toast.setGravity(Gravity.TOP | Gravity.LEFT, 40, 60);
//toast.setDuration(Toast.LENGTH_LONG);
if (length.equalsIgnoreCase("short")) {
toast.setDuration(Toast.LENGTH_SHORT);
} else {
toast.setDuration(Toast.LENGTH_LONG);
}
toast.setView(layout);
toast.show();
}
创建这样的布局
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_layout_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/rectangle_fill_black_color"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/tv_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textColor="#ffffff"
android:textSize="17sp" />
</LinearLayout>
或
您可以使用,
Toast Toast=newtoast(Activity.this);toast.setView(“您的视图”)你可以阅读Toast的源代码:你可以使用,Toast-Toast=newtoast(Activity.this);toast.setView(“您的视图”)代码>像这样。你可以阅读Toast的源代码:我想使用ondraw..还有什么我可以做到的吗?是的,你可以使用ondraw来创建CustomView,而不是像Toast那样的结构。Android Toast将为您提供时间祝酒消息的优势,例如,如果您想使用onDraw,那么您也必须开发该逻辑。我想使用onDraw..我是否可以这样做?是的,您可以使用onDraw创建CustomView,而不是像Toast这样的结构。Android Toast将为您提供时间祝酒消息的优势,例如,如果您想使用onDraw,那么您还必须开发该逻辑。
Common.displayCustomToast(activity, message, "long");
Common.displayCustomToast(activity, context.getResources().getString(R.string.please_check_internet), "short");