用Toast显示自定义的view
1. 布局文件 l
具体的代码如下:
<RelativeLayout xmlns:android="schemas.android/apk/res/android"
xmlns:tools="schemas.android/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.st.cgq.MainActivity"
android:background="#fff000">
<TextView
android:id="@+id/title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_alignParentLeft="true"/>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title_tv" >
<LinearLayout
android:id="@+id/all_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
2. 设置显示自定义view的代码:
public class ToastUntils {
public static void showToast(Context mContext,String value){
View view= LayoutInflater.from(mContext).inflate(R.layout.toast_view, null);
TextView tvTextView=(TextView)view.findViewById(R.id.name_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.ast_y_offset);
mTN.mGravity = context.getResources().getInteger(
com.android.internal.fig_toastDefaultGravity);
}
Toast设置自定义view
/**
* Set the view to show.
* @see #getView
*/
public void setView(View view) {
mNextView = view;
}
显示的位置
/**
* Set the location at which the notification should appear on the screen.
* @see android.view.Gravity
* @see #getGravity
*/
public void 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}
*
*/
public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.ansient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.ssage);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
/**
* Make a standard toast that just contains a text view with the text from a resource.
*
* @param context The context to use. Usually your {@link android.app.Application}
* or {@link android.app.Activity} object.
* @param resId The resource id of the string resource to use. Can be formatted text.
* @param duration How long to display the message. Either {@link #LENGTH_SHORT} or
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论