android-fragments 全屏对话框片段与状态栏重叠

kuarbcqp  于 2022-11-14  发布在  Android
关注(0)|答案(4)|浏览(207)

我用官方指南创建了一个全屏对话框
问题是,我的工具栏与状态栏重叠,我不知道如何解决这个问题。

对话框片段

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后,它看起来像

cld4siwp

cld4siwp1#

问题出在transaction.add(containerId,fragment)部分。
您将其设置为:transaction.add(android.R.id.content, fragment),并且是将其设置为android.r.id.内容导致了重叠。
而是将其设置为调用活动中父级内容框架的ID。
例如,在我的代码中,主Activity父布局是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();
u1ehiz5o

u1ehiz5o2#

如果你想让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));
8ehkhllq

8ehkhllq3#

在删除了我发现的StatusBar的透明颜色后,现在可以在不使状态栏变白色的情况下添加填充。
我的DialogFragment的RelativeLayout现在看起来像这样。

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

应用程序/源代码/主文件/资源/值-v21/样式.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,我还没有在其他设备上测试过这一点。

h79rfbju

h79rfbju4#

只需将android:fitsSystemWindows=“true”应用到您的根视图组:

<FrameLayout
    
    android:fitsSystemWindows="true">
    
 <View .../>

相关问题