2016-06-16 7 views
1

以下のアクティビティでは、断片と画像があります。フラグメントは、その上に画像を持つ暗いアクションバーです。私はフラグメントとして左のスライドメニューを持っているようにしているので、私はあらゆるアクティビティでそれを持つことができます。ナビゲーションドロワーを追加するには?

MainActivity;

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.Button; 
import android.widget.TextView; 


public class MainActivity extends AppCompatActivity { 

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

メインアクティビティXML;

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 
    tools:context="com.example.bassammetwally.egyptianstreets.MainActivity" 
    android:background="#b12828"> 

    <fragment 
     android:layout_width="match_parent" 
     android:layout_height="75dp" 
     android:name="com.example.bassammetwally.egyptianstreets.Title_bar" 
     android:id="@+id/fragment" 
     tools:layout="@layout/title_barmenu" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" /> 
    <ImageView 
     android:layout_width="100dp" 
     android:layout_height="50dp" 
     android:background="@drawable/logo" 
     android:layout_alignTop="@+id/fragment" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="8dp"/> 

</RelativeLayout> 

タイトルバーのすべてのアクティビティにあるべきフラグメント。

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.ViewGroup; 
import android.view.View; 

public class Title_bar extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.title_barmenu, container, false); 
    } 
} 

タイトルバーフラグメントXML;

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#8b0404"> 

</LinearLayout> 

タイトルバーにナビゲーション用の引き出しを実装するにはどうすればよいですか?

+0

Androidのスタジオは一人一人が作成し、修正デフォルトのナビゲーションドロワー活性を有し、ゼロからナビゲーション引き出しをしようとすることは仕事のビットは、あなたが私に言わせれば –

+0

@CoolGuyCGですが、私は、複数のNavigationDrawer活動を作成する必要が文句を言いません引き出しの各アイテムについて?少し不十分なようです。 – ann

+0

ドロワのタイトルを 'Fragment'としたいのですか? –

答えて

1

は、レイアウトファイルのために、あなただけ

<?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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 


<!-- Main content when drawer is closed --> 
    <include 
     layout="@layout/app_bar_nav" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
<!-- The drawer, you can change the menu contents dynamically --> 
    <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:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header" 
     app:menu="@menu/menu_nav" /> 

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

を必要とし、実装は、このような単純なことができ、このような何かを試してみてください。

package com.example; 


public class NavigationDrawer extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_navigation_drawer); 
     //You should remove this if you have no intent of using it 
     //And if you uset it, to prevent double actionbars, use a style with no actionbar 
     //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);//I like setting custom actionbar 
     //setSupportActionBar(toolbar); 


     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, "Open drawer", "Close drawer"); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.note_home, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     return true;//or super.onOptionsItemSelected, false won't show menu 
    } 


    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 
     switch (id) { 
      case R.id.nav_camera: 
       break; 
      case R.id.nav_gallery: 
       break; 

      case R.id.nav_schedule: 
       break; 
      case R.id.nav_manage: 
       //do someting silly 
       break; 
     } 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 
1

組み込みのAndroidスタジオナビゲーションバーの操作を使用します。私はどのようにタイトルバーの部分(

getActionBar().setCustomView()
)を使用するか分からないが、すべてはすでに与えられている。 XMLにフラグメントを作成する必要はありません。

関連する問題