2016-06-12 1 views
0

ActivityNavigationViewで作成しました。 NavigationViewの項目を1つ選択すると、Fragmentが読み込まれます。 フラグメント上の重複アクションバー

この

Activity次のとおりです。

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{ 

private DrawerLayout drawer; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_apresenta_atividades); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.app_name, R.string.app_name); 
    drawer.addDrawerListener(toggle); 
    toggle.syncState(); 
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
} 
... 

@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    Fragment fragment = null; 
    Class fragmentClass; 
    ... 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    fragmentManager.beginTransaction().replace(R.id.layout, fragment).commit(); 
    item.setChecked(true); 
    setTitle(item.getTitle()); 
    drawer.closeDrawers(); 
    return true; 
} 
} 

これはMainActivityと複数のフラグメントのためのXMLです: enter image description here

I:それは一つの断片を開始

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
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:background="#ddffff" 
android:fitsSystemWindows="true"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <include 
     android:id="@+id/bar" 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    //Layout for FirstFragment 
    <RelativeLayout 
     android:id="@+id/layout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     ... 

    </RelativeLayout> 

    //Layout for SecondFragment 
    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     ... 

    </RelativeLayout> 
</LinearLayout> 

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="#ddffff" 
    app:headerLayout="@layout/nav_header_main" 
    app:menu="@menu/activity_main_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

2 ActionBarsがあります私の.xmlをどのように再編成すれば、上にActionBarを表示するだけでよいのか分かりません。おかげさまで

編集:app_bar_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
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="match_parent" 
android:fitsSystemWindows="true" 
android:id="@+id/co_layout" > 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <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> 
</android.support.design.widget.CoordinatorLayout> 

のstyles.xml:

<resources> 

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 
<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 

</resources> 
+0

私はあなたが 'ナビゲーションドロワーActivity'を使用している参照してください。このためには、 'NoActionBar'スタイルを使用する必要があります。マニフェストを確認して、あなたの 'activity'がどのようなスタイルを使用しているのか確認できますか? –

+0

私はすでにMainActivityにそのスタイルを使用しています。 – tomss

+0

Hmm ... '@ layout/app_bar_main'のレイアウトは何ですか? –

答えて

0

はあなたが二AppBarLayout要求しない、そのようにしてみてくださいことができます:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
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:id="@+id/root" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context=".activity.HomeActivity"> 

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:id="@+id/home_header_layout" 
     android:fitsSystemWindows="true"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" /> 

    </LinearLayout> 

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

     app:menu="@menu/homepage_leftdrawer"> 


    </android.support.design.widget.NavigationView> 

</android.support.v4.widget.DrawerLayout> 

を手動で追加する場合は、styles.xmlの属性を使用してください。

マイstyles.xml

<style name="AppTheme3" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">@color/ColorPrimary</item> 
    <item name="colorPrimaryDark">@color/ColorPrimaryDark</item> 
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> 
    <!-- Customize your theme here. --> 
</style> 

<style name="AppTheme3.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 

<style name="AppTheme3.AppBarTransparent" parent="ThemeOverlay.AppCompat.Dark" /> 

<style name="AppTheme3.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 

<style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle"> 
    <item name="color">@android:color/white</item> 
</style> 

そしてstyles-v21.xml

<style name="AppTheme3" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">@color/ColorPrimary</item> 
    <item name="colorPrimaryDark">@color/ColorPrimaryDark</item> 
    <item name="android:colorAccent">@color/ColorPrimary</item> 
    <item name="android:statusBarColor">@color/ColorPrimaryDark</item> 
    <item name="android:colorFocusedHighlight">@color/ColorPrimary</item> 
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> 
    <item name="android:windowTranslucentStatus">false</item> 
    <item name="colorControlHighlight">@color/ColorControlHighlight</item> 
    <item name="android:windowContentTransitions">true</item> 
    <!-- Customize your theme here. --> 
</style> 


<style name="AppTheme3.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 

<style name="AppTheme3.AppBarTransparent" parent="ThemeOverlay.AppCompat.Dark" > 
    <item name="color">@android:color/white</item> 
</style> 

<style name="AppTheme3.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 

<style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle"> 

    <item name="color">@android:color/white</item> 
</style> 
+0

もしそうすれば、どちらも消えます。 – tomss

+0

@tomssあなたのstyles.xmlを更新できますか?私の質問では、私は完全なxml –

+0

私はstyles.xmlを更新します。 – tomss

関連する問題