天天看点

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横屏下实现全屏效果