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

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

这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值