您的位置:首页 > 移动开发 > Android开发

[安卓开发] 完美解决Dialog不能全屏

2016-07-24 18:32 435 查看

为什么布局设置了match_parent还是不能全屏呢?

看dialog的show的源码:

public void show() {
if (mShowing) {
if (mDecor != null) {
if (mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {
mWindow.invalidatePanelMenu(Window.FEATURE_ACTION_BAR);
}
mDecor.setVisibility(View.VISIBLE);
}
return;
}

mCanceled = false;

if (!mCreated) {
dispatchOnCreate(null);
}

onStart();
mDecor = mWindow.getDecorView();

if (mActionBar == null && mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {
final ApplicationInfo info = mContext.getApplicationInfo();
mWindow.setDefaultIcon(info.icon);
mWindow.setDefaultLogo(info.logo);
mActionBar = new WindowDecorActionBar(this);
}

WindowManager.LayoutParams l = mWindow.getAttributes();
if ((l.softInputMode
& WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) == 0) {
WindowManager.LayoutParams nl = new WindowManager.LayoutParams();
nl.copyFrom(l);
nl.softInputMode |=
WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
l = nl;
}

try {
mWindowManager.addView(mDecor, l);
mShowing = true;

sendShowMessage();
} finally {
}
}


可以看出,大小在show的时候设置的。在show之前,即使动态改变这个layoutParams,也是不行的。

解决方法1:

首先style:

<style name="CommentStyle" parent="@android:style/Theme.Dialog">

<item name="android:windowFullscreen">true</item>
<item name="android:windowNoTitle">true</item>

</style>


dilaog里面的setContentView下面

getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);


方法二

dialog里面重写show()

@Override
public void show() {
super.show();
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
DisplayMetrics dm = new DisplayMetrics();
WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
manager.getDefaultDisplay().getMetrics(dm);
layoutParams.width = dm.widthPixels;
layoutParams.height = dm.heightPixels;
view.setLayoutParams(layoutParams);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息