我正在创建我的自定义吐司主要是因为我将它放在网格视图上并需要对其宽度进行一些控制。但是我确实喜欢标准吐司上的浅蓝色光环,并希望得到它(或至少让我的自定义吐司看起来有点类似于默认吐司)。
实现这一目标的简单/优雅方式是什么?
我从这个布局创建了一个自定义吐司:
<LinearLayout
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:padding="10dp">
<ImageView android:id="@+id/toast_layout_image"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/icon" android:layout_marginRight="10dp">
</ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/toast_layout_text"
android:textColor="#FFF" android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
</LinearLayout>
并以这种方式调用它(android dev。文档中概述的常用方法以及Web上的几个教程,唯一的区别是我使用网格视图并且每个toast根据其网格块放置位置):
int location[] = new int[2];
view.getLocationOnScreen(location);
LayoutInflater inflater = getLayoutInflater();
View toastLayout = inflater.inflate(R.layout.toast_layout,(ViewGroup)findViewById(R.id.toast_layout_root));
TextView textView = (TextView) toastLayout.findViewById(R.id.toast_layout_text);
textView.setText("This tile does not contain any valid data.");
int toastWidth = cellWidth * 3 / 4;
textView.setWidth(toastWidth - 64); // TODO picture size needs to be correct
Toast toast = new Toast(getApplicationContext());
toast.setView(toastLayout);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.LEFT, location[0]+((cellWidth-toastWidth)/2),location[1]+cellHeight-100);
toast.show();
这看起来很干净但与默认烤面包完全不同..
答案 0 :(得分:4)
为您的Toast消息应用此布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="35dp"
android:orientation="horizontal"
android:background="@android:drawable/dialog_holo_dark_frame" >
<ImageView android:id="@+id/toast_layout_image"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/appicon" android:layout_marginRight="10dp">
</ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/toast_layout_text"
android:textColor="#FFF" android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
</LinearLayout>
我没有找到cellWidth和cellHeight是什么,所以如果它不适合,请尝试尝试填充 编辑:哦,我应用了黑暗主题,尝试使用光源代替。