2016-07-13 3 views
0

Android 앱을 개발 중이며 탐색 서랍이 있습니다. 내 질문은 오히려 모든 페이지에 같은 코드를 작성하는 것입니다 (나는 조각이 아닌 액티비티를 사용하기 때문에) 레이아웃 파일의 어딘가에 다른 파일에 포함시켜야합니다. 마찬가지로 :레이아웃 파일에 android.support.design.widget.NavigationView 포함

<android.support.design.widget.NavigationView 
android:id="@+id/navigation_view" 
android:layout_height="match_parent" 
android:layout_width="match_parent" 
android:layout_gravity="start" 
app:headerLayout="@layout/nav_header" 
app:menu="@menu/drawer_view" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:app="http://schemas.android.com/apk/res/android" 
xmlns:android="http://schemas.android.com/tools" /> 

이 BTW는 레이아웃의 높이와 너비를 정의하는 오류를 보여줍니다 이 같은 탐색 모음의 레이아웃 파일이 있습니다.

그리고 난 같은 다른 레이아웃 파일에 동일한 포함해야

<include layout="@layout/navigation_view" /> 

어떻게 그렇게?

답변

0

BaseActivity를 만들고 다른 활동을 확장 할 수 있습니다. 이 아이디어는 Google의 iosched 앱에서 사용되었습니다. NavigationView는 사용하지 않지만 액티비티의 계층 구조에 대한 주요 아이디어를 얻을 수 있습니다. 당신이 필요로하는 경우

기본적으로, BaseActivity의 레이아웃은 물론

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

<include layout="@layout/content_main" /> 

<android.support.design.widget.NavigationView 
    android:id="@+id/left_drawer" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header_main" 
    app:menu="@menu/activity_main_drawer" /> 
</android.support.v4.widget.DrawerLayout> 

과 같아야합니다, 당신은 appbar을 추가 할 수 있습니다.

0

xml 파일에 FrameLayout을 추가하십시오. 예 : 수입 추가

:

import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 

을 그리고 코드를 추가

FragmentManager mFragmentManager; 
FragmentTransaction mFragmentTransaction; 

mFragmentManager = getSupportFragmentManager(); 
mFragmentTransaction = mFragmentManager.beginTransaction(); 

을 그리고 마지막으로 FrameLayout이 대체 활동 파일 있도록

<FrameLayout 
      android:id="@+id/myView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"/> 

당신은 조각을 사용하는 것처럼 단편 :

mFragmentTransaction.replace(R.id.myView, new Fragment()).commit();