gpt4 book ai didi

java - 如何使DialogFragment全屏而不重叠状态栏/导航栏并保持状态栏颜色?

转载 作者:行者123 更新时间:2023-12-02 03:10:56 26 4
gpt4 key购买 nike

我已经在谷歌上搜索了几个小时并尝试了无数的方法,但我就是无法让它发挥作用......

这是MyFragment.java:

public class MyFragment extends DialogFragment {

public MyFragment() {
// Required empty public constructor
}

public static MyFragment newInstance() {
MyFragment newFragment = new LoginDialogFragment();
newFragment.setCancelable(false);
return newFragment;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), R.style.MyAppTheme_Dialog);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
ButterKnife.bind(this, view);
return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

Window window = getDialog().getWindow();

if (window != null) {
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
window.setWindowAnimations(R.style.MyAppTheme_Dialog);
}
}

}

这是styles.xml:

<style name="MyAppTheme" parent="PreferenceFixTheme.Light.NoActionBar">
<item name="android:windowBackground">@color/background</item>
<item name="android:colorBackground">@color/background</item>
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
<item name="preferenceCategory_marginBottom">0dp</item>
</style>

<style name="MyAppTheme.Dialog">
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowBackground">@null</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowEnterAnimation">@anim/slide_up</item>
<item name="android:windowExitAnimation">@anim/slide_down</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>

这几乎按照我的预期工作...唯一的问题是调用 window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT); 来填充整个屏幕(减去状态/导航栏)将状态栏颜色更改为黑色。当我关闭对话框时,它会变回 colorPrimaryDark

如果我删除 window.setLayout(...) 行,状态栏将保留 colorPrimaryDark 但对话框大小将是默认值(一个小方 block )在屏幕中间)。

在我调用 window.setLayout(...) 的同一位置,我尝试放置以下代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(ContextCompat.getColor(getActivity, R.color.primary_dark));
}

这会将状态栏颜色设置为 colorPrimaryDark,但它会与状态栏和导航栏重叠。

如果不重叠状态栏和导航栏并将 colorPrimaryDark 保留在状态栏上,我就无法获得全屏。

我发现了一些与同样问题有关的问题,但我无法让建议发挥作用。两者都没有公认的答案:

最佳答案

查看这个自定义的对话框 fragment ,尝试一下,它会满足您的需求。并让我知道。

 import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.TypedValue;
import android.view.Display;

/**
* Created by Noorul on 02-07-16.
*/

public class _DialogFragment extends DialogFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// preventScreenRotation();
}

@Override
public void onDestroy() {
// releaseScreenRotation();
super.onDestroy();
}

@SuppressWarnings("deprecation")
@Override
public void onStart() {
super.onStart();

// change dialog width
if (getDialog() != null) {

int fullWidth = getDialog().getWindow().getAttributes().width;
int fullHeight = getDialog().getWindow().getAttributes().height;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);

fullWidth = size.x;
fullHeight = size.y;
} else {
Display display = getActivity().getWindowManager().getDefaultDisplay();
fullWidth = display.getWidth();
fullHeight = display.getHeight();
}

final int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0, getResources()
.getDisplayMetrics());

int w = fullWidth - padding;
int h = fullHeight - (padding * 2);

getDialog().getWindow().setLayout(w, h);
}
}


private void preventScreenRotation() {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}

private void releaseScreenRotation() {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}

根据您的需要更改对话框的大小。

关于java - 如何使DialogFragment全屏而不重叠状态栏/导航栏并保持状态栏颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41072037/

26 4 0