文档详情

用Toast显示自定义的view.docx

发布:2017-12-12约3.84千字共4页下载文档
文本预览下载声明
用Toast显示自定义的view布局文件toast_view.xml具体的代码如下:RelativeLayoutxmlns:android=/apk/res/androidxmlns:tools=/toolsandroid:layout_width=match_parentandroid:layout_height=match_parentandroid:paddingBottom=@dimen/activity_vertical_marginandroid:paddingLeft=@dimen/activity_horizontal_marginandroid:paddingRight=@dimen/activity_horizontal_marginandroid:paddingTop=@dimen/activity_vertical_margintools:context=com.shunchang.yingyong.test.cgq.MainActivityandroid:background=#fff000TextViewandroid:id=@+id/title_tvandroid:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:text=@string/hello_worldandroid:layout_alignParentLeft=true/ScrollViewandroid:id=@+id/scrollView1android:layout_width=match_parentandroid:layout_height=wrap_contentandroid:layout_below=@+id/title_tv LinearLayoutandroid:id=@+id/all_llandroid:layout_width=match_parentandroid:layout_height=match_parentandroid:orientation=verticalTextViewandroid:id=@+id/name_tvandroid:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:text=@string/hello_world//LinearLayout/ScrollView/RelativeLayout设置显示自定义view的代码:publicclassToastUntils {publicstaticvoidshowToast(Context mContext,Stringvalue){View view= LayoutInflater.from(mContext).inflate(R.layout.toast_view, null);TextViewtvTextView=(TextView)view.findViewById(R._tv);tvTextView.setText(value);Toast toast=newToast(mContext);toast.setView(view);toast.setGravity(Gravity.CENTER, 0, 0);toast.show();}}调用过程:ToastUntils.showToast(MainActivity.this,”要显示的内容”);Toast 源码解析:Toast 的构造防范如下public Toast(Context context) {mContext = context;mTN = new TN();mTN.mY = context.getResources().getDimensionPixelSize(ernal.R.dimen.toast_y_offset);mTN.mGravity = context.getResources().getInteger(ernal.R.integer.config_toastDefaultGravity); }Toast设置自定义view/** * Set the view to show. * @see #getView */publicvoidsetView(View view) {mNextView = view; }显示的位置/** * Set the location at which the notification should appear on the screen. * @seeandroid.view.Gravity *
显示全部
相似文档