自定義全屏dialog
阿新 • • 發佈:2019-02-01
有些時候需要彈出不同的dialog,這裡使用到了一種全屏的dialog,效果圖如下:(呵呵,可以下載我們的app哦)
具體程式碼:
java呼叫程式碼:
QuestionDialog1 mAraltDialog = new QuestionDialog1(SettingAboutView.this);
mAraltDialog.setTitle("幫助中心");
mAraltDialog.ShowDialog(HttpUtil.BAOFOO_AGREEMENT_BASE_ABOUT_URL);
全屏dialog程式碼:
動畫R.style.mystyle程式碼:package com.baofoo.mobile.view.dialog; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.TextView; import com.baofoo.mobile.wallet.R; import com.baofoo.mobile.webview.WebContentView; public class QuestionDialog1 extends AlertDialog { private View rootview; WebContentView mWebView; private TextView mTitleClose; private TextView midTitle; NormalProgressDialog loadingDialog; public interface QuestionDialogInterface { public void dismisss(); public void onItemClick(int pos); } public QuestionDialogInterface mListener; // public void setListener(QuestionDialogInterface istener){ // mListener = istener; // } public QuestionDialog1(Context context) { // super(context, R.style.DialogStyle); super(context); rootview = LayoutInflater.from(context).inflate(R.layout.question_dialog1, null, false); loadingDialog = new NormalProgressDialog(context); midTitle = (TextView) rootview.findViewById(R.id.midTitle); mWebView = (WebContentView) rootview.findViewById(R.id.webContentView1); mWebView.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); loadingDialog.dismiss(); } }); mTitleClose = (TextView) rootview.findViewById(R.id.title_close_text); mTitleClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // mListener.dismisss(); dismiss(); } }); setOnDismissListener(new OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { if (loadingDialog != null) { loadingDialog.dismiss(); } } }); } public void setTitle(String title) { midTitle.setText(title); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(rootview); // this.getWindow().setBackgroundDrawable(new // PaintDrawable(Color.TRANSPARENT)); setCancelable(true); setCanceledOnTouchOutside(false); } /** * 顯示本dialog * 外部物件呼叫 * * @param url 需要現實的網頁 */ public void ShowDialog(String url) { if (this.isShowing()) return; if (mWebView != null) { mWebView.loadUrl(url); } Window window = this.getWindow(); window.setGravity(Gravity.BOTTOM); // 此處可以設定dialog顯示的位置 window.setWindowAnimations(R.style.mystyle); // 新增動畫 // 兩句的順序不能調換 this.show(); WindowManager.LayoutParams lp = window.getAttributes(); lp.width = WindowManager.LayoutParams.FILL_PARENT; lp.height = WindowManager.LayoutParams.FILL_PARENT; window.setAttributes(lp); window.getDecorView().setPadding(0, 0, 0, 0); } }
佈局檔案程式碼:<style name="mystyle" parent="android:Animation"> <item name="@android:windowEnterAnimation">@anim/dialog_enter</item> //進入時的動畫 <item name="@android:windowExitAnimation">@anim/dialog_exit</item> //退出時的動畫 </style>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/plugin_main_title" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="47dp" android:background="#2E8FD3" android:gravity="center_vertical" > <TextView android:id="@+id/midTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@null" android:text="@string/app_name" android:textColor="@color/font_white" android:textSize="@dimen/fontsize22" /> <TextView android:id="@+id/title_close_text" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentRight="true" android:gravity="center" android:paddingLeft="10dp" android:paddingRight="10dp" android:text="關閉" android:textColor="@color/font_white"/> </RelativeLayout> <com.baofoo.mobile.webview.WebContentView android:id="@+id/webContentView1" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>