2016-03-17 10 views
6

ich mit den official GuideVollbild DialogFragment überlappt mit StatusBar

Das Problem ist, einen Vollbild-Dialog erstellt, dass meine Toolbar überlappt mit der Statusleiste, und ich kann nicht herausfinden, wie diese zu lösen.

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

Layout-

<?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> 

Toolbar

<?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> 

Aktivität Erstellen Dialog

@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

EDIT

Nach StatusBarTransparency Entfernen von v21-Stile es ähnliche Nach dem Entfernen enter image description here

+0

Fügen Sie Ihre 'layout/toolbar.xml' hinzu, wenn Sie möchten. –

+0

@AlexTownsend Ist enthalten, danke für das Bemerken. – Altoyyr

+1

Ist Ihr Aktivitätslayout '' fitsSystemWindows = "true" 'überall? –

Antwort

2

sieht die Transparenzfarbe der StatusBar Ich habe festgestellt, dass es jetzt möglich ist, eine Auffüllung hinzuzufügen, ohne dass die Statusleiste weiß wird.

Die RelativeLayout meiner DialogFragment sieht nun wie folgt.

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

app/src/main/res/Werte-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> 

Warnung
Ich habe das nicht auf anderen Geräten als dem Nexus getestet 5.

4

Ich hatte dieses genaue Problem und es geschafft, es zu lösen. Das Problem liegt im transaction.add-Teil (containerId, fragment).

Sie haben es eingestellt: transaction.add(android.R.id.content, fragment), und es ist die Einstellung von es android.R.id.content, die die Überlappung verursacht.

Stattdessen setzen sie auf die ID des Inhaltsbereichs der Eltern in der anrufenden Aktivität.

Zum Beispiel in meinem Code der Haupttätigkeit Eltern Layout war ein DrawerLayout, mit der ID von drawer_layout, so meine fix war

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

Ich hoffe, es hilft Ihnen, oder jemand anderes für diese Lösung zu suchen.

+0

perfekte Lösung –