android上dialog横屏下实现全屏效果
16lz
2021-01-23
其实在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(); }}
更多相关文章
- Android利用drawable文件夹自定义控件背景、样式
- Android 设定横屏,禁止屏幕旋转,Activity重置 [更新视频播放器相关
- 【Android Training UI】创建自定义Views(Lesson 0 - 章节概览)
- 【 Android】使手机屏幕常亮,不进入待机状态
- Android 自定义TextView 实现文本间距
- Android中屏幕相关的操作
- Android中自定义switch控件样式