【问题标题】:Fullscreen DialogFragment overlaps with StatusBar全屏 DialogFragment 与 StatusBar 重叠
【发布时间】:2016-07-04 04:36:20
【问题描述】:

我用official Guide 创建了一个全屏对话框

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

对话框片段

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>

工具栏

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                            xmlns:app="http://schemas.android.com/apk/res-auto"
                                            xmlns:tools="http://schemas.android.com/tools"
                                            android:layout_width="match_parent"
                                            android:layout_height="wrap_content"
                                            android:theme="@style/AppTheme.AppBarOverlay"
                                            tools:showIn="@layout/activity_login">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

活动创建对话框

@Subscribe
public void showNewAccountDialog(OttoMessages.Login.ShowNewAccountDialog evt) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    CreateAccountDialogFragment newFragment =
            new CreateAccountDialogFragment();

    boolean isLargeLayout = false;
    if (isLargeLayout) {
        newFragment.show(fragmentManager, "dialog");
    } else {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.add(android.R.id.content, newFragment)
                .addToBackStack(null).commit();
    }
}

编辑

从 v21 样式中删除 StatusBarTransparency 后,它看起来像这样

【问题讨论】:

  • 如果可以的话,请包括您的layout/toolbar.xml
  • @AlexTownsend 已包含,感谢您的关注。
  • 您的活动布局是否在任何地方使用fitsSystemWindows="true"
  • @AlexTownsend 不,不是,如果我添加fitsSystemWindows="true",工具栏会向下滑动,但状态栏只是一个白色的空白栏
  • 检查&lt;item name="android:statusBarColor"&gt;@android:color/transparent&lt;/item&gt; 行是否有values/styles-v21/styles.xml。如果你有它,也尝试删除它。

标签: java android android-fragments dialog dialogfragment


【解决方案1】:

问题出在transaction.add(containerId, fragment)部分。

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

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

例如,在我的代码中,主要活动的父布局是 DrawerLayout,id 为drawer_layout,所以我的修复是

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

【讨论】:

    【解决方案2】:

    如果您希望 DialogFragment 全屏显示并且还希望状态栏具有您自己的颜色,您可以在 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));
    

    【讨论】:

    • 很好,clearFlags 适合我,但setStatusBarColor 不适合(Android 9)。
    【解决方案3】:

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

    我的 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,我还没有在其他设备上测试过。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多