2016-03-17 117 views
6

重叠我创建了一个全屏幕的对话框与official Guide全屏DialogFragment与状态栏

的问题是,我的工具栏与状态栏重叠,我无法弄清楚如何解决这个问题。

DialogFragment

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(); 
    } 
} 

Screenshot

编辑

从V21的样式看起来这 enter image description here

+0

如果可以的话,请包括你的'layout/toolbar.xml'。 –

+0

@AlexTownsend包括在内,谢谢你的注意。 – Altoyyr

+1

您的活动布局是否在任何地方使用'fitsSystemWindows =“true”'? –

回答

2

取出后取出StatusBarTransparency后我发现StatusBar的透明度颜色,现在可以在没有状态栏变白的情况下添加填充。

我的DialogFragment的RelativeLayout现在看起来像这样。

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

应用程序/ SRC /主/ RES /值-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.

4

我有这个确切的问题,并设法解决它。问题出在transaction.add(containerId,fragment)部分。

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

而是将其设置为调用活动中父级内容框架的标识。

例如,在我的代码的主要活动父布局是DrawerLayout,与drawer_layout的ID,所以我定盘

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

我希望它可以帮助你或其他人寻找这种解决方案。

+0

完美的解决方案 –

相关问题