2016-04-01 7 views
0

私はアンドロイドのデフォルトのナビゲーションドロワーを使用しています。私はインターネット上で検索したすべてのアクティビティで同じレイアウトをしたいと思います。カスタマイズされた引き出しにはいくつかのアイデアがありますが、アンドロイドスタジオのデフォルトの引き出しにはありません。親切に私を助けてください。diffフラグメントのナビゲーションドロット

ナビゲーションdrawer.class:

public class MainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main2); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) 
    findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", 
    Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, 
    R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

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

がapp_main.xml:

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

<include 
    layout="@layout/app_bar_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<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_main" 
    app:menu="@menu/activity_main_drawer" /> 

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

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" 
tools:context="com.spykid.navig.navig_sample.Main2Activity"> 

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

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

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/ic_dialog_email" /> 

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

ここapp_bar_mainレイアウトで、私は が含まれていることを変更したいですlayout = "@ layout/content_main"を自分のアクティビティやフラグメントと組み合わせて私は同じレイアウトを持つことができます。

ありがとうございます。

+0

質問が混乱しているようです – Lokanath

+0

新しいアクティビティを作成する場合、同じナビゲーションドロワーを使用する方法が必要です –

+0

ちょっと試してみてくださいk [http://androhub.com/android-navigationdrawer/]をクリックしてカスタムナビゲーションボックスを開きます。 –

答えて

0

NavigationDrawerのみを含むアクティビティを作成した後、OnItemSelectedからNavigationDrawerに電話してください。その後、すべてのアクティビティでNavigationDrawerが利用可能になります。

MainActivityNavigationDrawerと作成し、Activitiesの代わりにFragmentsを作成することです。 FragmnetsOnItemSelectedNavigationDrawerから呼び出します。そこにはたくさんの例があります。あなたがnavigationDrawerを定義しているあなたの活動に

編集

、次の操作を行います。そして、代わりに次のレイアウトのための新たな活動を作成するのではなく、断片を作成し、以下のようにnavigationDrawerにOnItemSelectedにそれらを呼び出す:

public class ArtistsFragment extends Fragment { 

    public ArtistsFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    } 

    @Override 
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View rootview= inflater.inflate(R.layout.fragment_artists, container, false); 

     return rootview; 
    }} 

activity_main.xml:

<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:elevation="4dp" 
    android:layout_height="fill_parent" 
    > 


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

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


     <FrameLayout 
      android:id="@+id/content_frame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/home"> 


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

       > 



</LinearLayout> 


     </FrameLayout> 



    </LinearLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     android:background="@drawable/bg_all" 
     app:itemIconTint="@android:color/white" 
     app:itemTextColor="@android:color/white" 
     app:theme="@style/list_item_appearance" 
     app:menu="@menu/drawer_menu" > 


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

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 

      // This method will trigger on item Click of navigation menu 
      @Override 
      public boolean onNavigationItemSelected(MenuItem menuItem) { 


       //Checking if the item is in checked state or not, if not make it in checked state 
       if (menuItem.isChecked()) menuItem.setChecked(false); 
       else menuItem.setChecked(true); 

       //Closing drawer on item click 
       drawerLayout.closeDrawers(); 

       //Check to see which item was being clicked and perform appropriate action 
       switch (menuItem.getItemId()) { 


        //Replacing the main content with ContentFragment Which is our Inbox View; 
        case R.id.drawer_home: 

         Intent intent=new Intent(HomeActivity.this, HomeActivity.class); 
         startActivity(intent); 
         overridePendingTransition(0, 0); 
         finish(); 
         return true; 

        // For rest of the options we just show a toast on click 

        case R.id.drawer_artist: 

         android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager(); 
         android.support.v4.app.FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); 
         fragmentTransaction.replace(R.id.content_frame, new ArtistsFragment()); 
         fragmentTransaction.addToBackStack(null); 
         fragmentTransaction.commit(); 
         return true; 


        default: 
         Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show(); 
         return true; 

       } 
      } 
     }); 

フラグメントのコードは次のようになります

+0

私は私の活動をintent startactivityによってonItemselectedから呼び出す場合、私はナビゲーションドロワーを取得していません –

+0

@ Nikhil-あなたのナビゲーションドロワーを別のアクティビティに入れましたか?あなたが混乱している場合は、断片のために試してみませんか? –

+0

@Nikhilもしあなたが私にあなたのための完全なコードを提供したいならば。 –