2015-08-27 2 views
3

Я пытаюсь реализовать материала навигации ящик, используяNavigationView и панель инструментов, накладывая по содержанию

  • android.support.v4.widget.DrawerLayout

  • android.support.v7.widget.Toolbar

  • android.support.design.widget.NavigationView

Проблема заключается в том, что NavigationView & Панель инструментов всегда накладывается FrameContent. FrameContent просто фрагмент.

* ПРИМЕЧАНИЕ: Я попытался заменить LinearLayout на RelativeLayout как контейнер, но не работал.

activity_layout.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawer" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <FrameLayout 
     android:id="@android:id/content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/toolbar"/> 
    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     android:layout_alignParentTop="true"/> 
</RelativeLayout> 

<android.support.design.widget.NavigationView 
    android:id="@+id/navigation_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start"/></android.support.v4.widget.DrawerLayout> 

Fragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 

<TextView android:text="@string/hello_world" android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/></FrameLayout> 

ActivityMain.java

public class MainActivity extends AppCompatActivity{ 

protected Toolbar mToolbar; 
protected NavigationView mNavigationView; 
protected DrawerLayout mDrawerLayout; 
protected FrameLayout container; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.material_activity_layout); 

    this.mToolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(this.mToolbar); 

    this.mNavigationView = (NavigationView) findViewById(R.id.navigation_view); 

    this.mNavigationView.inflateHeaderView(R.layout.nav_header); 
    this.mNavigationView.inflateMenu(R.menu.drawer); 

    this.mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer); 
    ActionBarDrawerToggle defaultActionBarDrawerToggle = new ActionBarDrawerToggle(
      this, 
      this.mDrawerLayout, 
      this.mToolbar, 
      R.string.app_name, 
      R.string.app_name) { 
     @Override 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
     } 

     @Override 
     public void onDrawerClosed(View drawerView) { 
      super.onDrawerClosed(drawerView); 
     } 
    }; 

    this.mDrawerLayout.setDrawerListener(defaultActionBarDrawerToggle); 

    defaultActionBarDrawerToggle.syncState(); 

    this.mToolbar.setTitle("Test Toolbar"); 
    this.mToolbar.setBackgroundColor(Color.BLUE); 

    getFragmentManager().beginTransaction().add(android.R.id.content, new InFragment(), "fragment").commit(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    return true; 
} 

public static class InFragment extends Fragment { 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     return inflater.inflate(R.layout.fragment, container, false); 
    } 
} 

}

Здесь результат

result 1

result 2

ответ

1

Вы не должны переопределять @android: идентификатор/содержание потому что это может вызвать некоторые странное поведение так просто ввести новый идентификатор, такой как @ + id/fragmentcontainer и использовать его.

Когда вы добавляете фрагмент с android.R.id.content и уже есть некоторый контент, он будет размещен над другим контентом.

изменить также порядок, в котором панели находится перед остальной частью контента, как в framelayout и т.д.

Надеются, что это помогает

Edit: я сделал адаптировать мой старый ответ, так как он не было 100% правильно ,

-1

Вы можете использовать LinearLayout с android:orientation="vertical" вместо RelativeLayout

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    android:layout_alignParentTop="true"/> 
    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
    </FrameLayout> 
</LinearLayout> 
+0

как я заметил, я пробовал, ни работа –

+0

, но почему вы создаете еще один FrameLayoutin в frgment. Создайте новый андроид: id = "@ + id/content" 'вместо android: id =" @ android: id/content " –

+0

Я отредактировал ответ.! –

Смежные вопросы