天天看點

android上dialog橫屏下實作全屏效果

其實在android上實作全屏效果也是很簡單滴,主要用到了android為我們提供的樣式,下面我貼代碼了,算是自己的一個記錄。

定義樣式檔案

在styles.xml中定義如下兩個樣式:

<style name="preview_dialog" parent="@android:style/Theme.Material.Light.Dialog">
        <item name="android:windowTranslucentNavigation">false</item>
</style>

<style name="fullScreen_dialog" parent="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen">
        <item name="android:windowTranslucentNavigation">false</item>
</style>
           

顯示dialog

int currentOrientation = MainActivity.this.getResources().getConfiguration().orientation;
// 根據目前的螢幕是否橫屏,切換目前需要用到的樣式
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
    mCurrentStyle = R.style.fullScreen_dialog;
} else {
    mCurrentStyle = R.style.preview_dialog;
}
    if (null != mPreviewDialog) {
        mPreviewDialog.dismiss();
        mPreviewDialog = null;
    }

    showPreviewDialog();
    mIsShowPreview = true;
           

showPreviewDialog

showPreviewDialog是用來顯示dialog的方法。

private Dialog mPreviewDialog = null;
private String mContent = "this is dialog content.....";
private Drawable mAlertIcon = null;
private int mCurrentStyle;
private boolean mIsShowPreview = false;

public void showPreviewDialog() {
        mPreviewDialog = new Dialog(MainActivity.this,mCurrentStyle);
        mPreviewDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View alertReimderView = LayoutInflater.from(MainActivity.this).inflate(
                R.layout.cell_broadcast_reminder, null);
        ((TextView) alertReimderView.findViewById(R.id.alertTitle))
            .setText(R.string.app_name);
        ((ImageView) alertReimderView.findViewById(R.id.icon))
            .setImageDrawable(getResources().getDrawable(R.drawable.ic_default_contact));
         ((TextView) alertReimderView.findViewById(R.id.message))
            .setText(mContent);
         Button okBtn = (Button) alertReimderView.findViewById(R.id.dismissButton);
         okBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != mPreviewDialog) {
                    mPreviewDialog.dismiss();
                    mPreviewDialog = null;
                    mIsShowPreview = false;
                }
            }
        });
         mPreviewDialog.setContentView(alertReimderView, new ViewGroup.LayoutParams(
                 ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
         mPreviewDialog.setCancelable(false);
         mPreviewDialog.show();
}
           

監聽切換螢幕方向

需要在目前的activity中添加如下配置:

  • 重寫onConfigurationChanged方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.d(TAG, "the secondactivity onconfiguration runs newConfig.orientation is :"+newConfig.orientation);
        if (mIsShowPreview) {
            if (newConfig.orientation==Configuration.ORIENTATION_PORTRAIT) {
                if (mCurrentStyle == R.style.fullScreen_dialog) {
                    mCurrentStyle = R.style.preview_dialog;

                }
            }  
            if (newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE) {
                if (mCurrentStyle == R.style.preview_dialog) {
                    mCurrentStyle = R.style.fullScreen_dialog;
                }
            }
            if (null != mPreviewDialog) {
                mPreviewDialog.dismiss();
                mPreviewDialog = null;
            }
            showPreviewDialog();
        }
}
           
android上dialog橫屏下實作全屏效果