Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 全屏对话框片段与状态栏重叠_Java_Android_Android Fragments_Dialog_Dialogfragment - Fatal编程技术网

Java 全屏对话框片段与状态栏重叠

Java 全屏对话框片段与状态栏重叠,java,android,android-fragments,dialog,dialogfragment,Java,Android,Android Fragments,Dialog,Dialogfragment,我创建了一个全屏对话框 问题是,我的工具栏与状态栏重叠,我不知道如何解决这个问题 对话框片段 public class CreateAccountDialogFragment extends BaseDialogFragment { @Inject CreateAccountViewModel viewModel; @Override public View onCreateView(LayoutInflater inflater, ViewGroup con

我创建了一个全屏对话框

问题是,我的工具栏与状态栏重叠,我不知道如何解决这个问题

对话框片段

public class CreateAccountDialogFragment extends BaseDialogFragment {

    @Inject
    CreateAccountViewModel viewModel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //InjectDependencies....
        View rootView = createDataBinding(inflater, container);
        createToolbar(rootView);

        return rootView;
    }

    private View createDataBinding(LayoutInflater inflater, ViewGroup container) {
        CreateAccountDialogFragmentBinding binding =
                DataBindingUtil.inflate(inflater, R.layout.create_account_dialog_fragment, container, false);
        binding.setViewModel(viewModel);
        return binding.getRoot();
    }

    private void createToolbar(View rootView) {
        Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);

        // Set an OnMenuItemClickListener to handle menu item clicks
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.action_save) {
                    viewModel.createAccount();
                }
                return true;
            }
        });

        // Inflate a menu to be displayed in the toolbar
        toolbar.inflateMenu(R.menu.create_account_menu);
        toolbar.setTitle(R.string.create_account);
        toolbar.setNavigationIcon(R.drawable.ic_cancel);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogFragment.dismiss();
            }
        });
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="viewModel"
            type="org.altoware.weity.viewmodels.CreateAccountViewModel"/>
    </data>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_light">

        <include layout="@layout/toolbar"/>

        <LinearLayout
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical">

            <org.altoware.weity.views.BindableEditText
                android:id="@+id/editTextUsername"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:addTextChangedListener="@{viewModel.onUsernameChanged}"
                android:hint="Username"
                android:singleLine="true"/>

            <org.altoware.weity.views.BindableEditText
                android:id="@+id/editTextPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:addTextChangedListener="@{viewModel.onPasswordChanged}"
                android:hint="Password"
                android:inputType="textPassword"
                android:singleLine="true"/>

        </LinearLayout>
    </RelativeLayout>
布局

public class CreateAccountDialogFragment extends BaseDialogFragment {

    @Inject
    CreateAccountViewModel viewModel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //InjectDependencies....
        View rootView = createDataBinding(inflater, container);
        createToolbar(rootView);

        return rootView;
    }

    private View createDataBinding(LayoutInflater inflater, ViewGroup container) {
        CreateAccountDialogFragmentBinding binding =
                DataBindingUtil.inflate(inflater, R.layout.create_account_dialog_fragment, container, false);
        binding.setViewModel(viewModel);
        return binding.getRoot();
    }

    private void createToolbar(View rootView) {
        Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);

        // Set an OnMenuItemClickListener to handle menu item clicks
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.action_save) {
                    viewModel.createAccount();
                }
                return true;
            }
        });

        // Inflate a menu to be displayed in the toolbar
        toolbar.inflateMenu(R.menu.create_account_menu);
        toolbar.setTitle(R.string.create_account);
        toolbar.setNavigationIcon(R.drawable.ic_cancel);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogFragment.dismiss();
            }
        });
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="viewModel"
            type="org.altoware.weity.viewmodels.CreateAccountViewModel"/>
    </data>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_light">

        <include layout="@layout/toolbar"/>

        <LinearLayout
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical">

            <org.altoware.weity.views.BindableEditText
                android:id="@+id/editTextUsername"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:addTextChangedListener="@{viewModel.onUsernameChanged}"
                android:hint="Username"
                android:singleLine="true"/>

            <org.altoware.weity.views.BindableEditText
                android:id="@+id/editTextPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:addTextChangedListener="@{viewModel.onPasswordChanged}"
                android:hint="Password"
                android:inputType="textPassword"
                android:singleLine="true"/>

        </LinearLayout>
    </RelativeLayout>

编辑

从v21样式中删除StatusBarTransparency后,看起来是这样的

删除状态栏的透明颜色后,我发现现在可以添加填充,而状态栏不会变白

我的DialogFragment的
RelativeLayout
现在如下所示

 <RelativeLayout
      android:paddingTop="24dp"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@android:color/background_light">

app/src/main/res/values-v21/styles.xml

<resources>>
      <style name="AppTheme.NoActionBar">
          <item name="windowActionBar">false</item>
          <item name="windowNoTitle">true</item>
          <item name="android:windowDrawsSystemBarBackgrounds">true</item>

          <!-- REMOVE THIS LINE 
          <item name="android:statusBarColor">@android:color/transparent</item>
          -->
      </style>
  </resources>
>
假的
真的
真的
警告

除了Nexus 5,我还没有在其他设备上测试过这个问题。

问题出在事务中。添加(containerId,fragment)部分

您已将其设置为:
transaction.add(android.R.id.content,fragment)
,是将其设置为android.R.id.content导致重叠

相反,将其设置为调用活动中父级内容框架的id

例如,在我的代码中,主活动父布局是一个抽屉布局,id为drawer_布局,因此我的修复是

 MyDialogFragment fragment = new MyDialogFragment ();
 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
 transaction.add(R.id.drawer_layout, frag)
            .addToBackStack(null).commit();

如果您希望DialogFragment全屏显示,并且希望statusbar具有您自己的颜色,您可以在DialogFragment的onCreate()方法中添加此选项

 setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen);
您还可以在onCreateView方法中为状态栏颜色添加以下代码

 getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
 getDialog().getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));

如果可以,请包括您的
布局/toolbar.xml
。@AlexTownsend包括在内,谢谢您的注意。您的活动布局是否使用
FitsystemWindows=“true”
任何地方?@AlexTownsend不,如果我添加
FitsystemWindows=“true”
工具栏向下滑动,但状态栏只是一个白色空白条检查是否有
values/styles-v21/styles.xml
@android:color/transparent
。如果有的话,也可以尝试删除它。很好,
clearfags
对我很有效,但是
setStatusBarColor
不适用(Android 9)。