用Toast显示自定义的view
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
用Toast显示自定义的view
1.布局文件toast_view.xml
具体的代码如下:
xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.shunchang.yingyong.test.cgq.MainActivity" android:background="#fff000"> android:id="@+id/title_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:layout_alignParentLeft="true"/> android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/title_tv" > android:id="@+id/all_ll" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> android:id="@+id/name_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world"/>
2.设置显示自定义view的代码:
publicclass ToastUntils {
publicstaticvoid showToast(Context mContext,String value){ View view=
LayoutInflater.from(mContext).inflate(yout.toast_view, null);
TextView tvTextView=(TextView)view.findViewById(_tv);
tvTextView.setText(value);
Toast toast=new Toast(mContext);
toast.setView(view);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
3.调用过程:
ToastUntils.showToast(MainActivity.this,”要显示的内容”);
4.Toast 源码解析:
Toast 的构造防范如下
public Toast(Context context) {
mContext = context;
mTN = new TN();
mTN.mY = context.getResources().getDimensionPixelSize(
com.android.internal.R.dimen.toast_y_offset);
mTN.mGravity = context.getResources().getInteger(
com.android.internal.R.integer.config_toastDefaultGravity);
}
Toast设置自定义view
/**
* Set the view to show.
* @see #getView
*/
publicvoid setView(View view) {
mNextView = view;
}
显示的位置
/**
* Set the location at which the notification should appear on the screen.
* @see android.view.Gravity
* @see #getGravity
*/
publicvoid setGravity(int gravity, int xOffset, int yOffset) {
mTN.mGravity = gravity;
mTN.mX = xOffset;
mTN.mY = yOffset;
}
常见的toast调用的方法:
/**
* Make a standard toast that just contains a text view.
*
* @param context The context to use. Usually your{@link
android.app.Application}
* or{@link android.app.Activity} object.
* @param text The text to show. Can be formatted text.
* @param duration How long to display the message. Either {@link #LENGTH_SHORT} or
* {@link #LENGTH_LONG}
*
*/
publicstatic Toast makeText(Context context, CharSequence text,
@Duration int duration) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(YOUT_INFLATER_SERVICE);
View v =
inflate.inflate(yout.transient_notification, null);
TextView tv =
(TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}