android上dialog横屏下实现全屏效果

来源:互联网 发布:simulink 电气端口 编辑:程序博客网 时间:2024/05/16 12:05

其实在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中添加如下配置:

android:configChanges="orientation|screenSize|keyboardHidden"
  • 重写onConfigurationChanged方法
@Overridepublic 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();        }}

这里写图片描述

1 0
原创粉丝点击