2016-05-10 15 views
0

私は1つのAndroidアプリケーションでアクションバー付きの引き出しナビゲーションを実装しました。今、私は代わりにアクションバーの代わりにツールバーを挿入する必要がありますので、非推奨です。しかし、私はこれを行うことができません。ActionBarの代わりにToolBarを設定するには

public class AndroidNavDrawerActivity extends AppCompatActivity 
     implements NavigationDrawerFragment.NavigationDrawerCallbacks { 

    private NavigationDrawerFragment mNavigationDrawerFragment; 
    private CharSequence mTitle; 

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

     mNavigationDrawerFragment = (NavigationDrawerFragment) 
       getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); 
     mTitle = getTitle(); 

     mNavigationDrawerFragment.setUp(
       R.id.navigation_drawer, 
       (DrawerLayout) findViewById(R.id.drawer_layout)); 
    } 

    @Override 
    public void onNavigationDrawerItemSelected(int position) { 
     Fragment fragment; 
     //FragmentManager fragmentManager = getSupportFragmentManager(); // For AppCompat use getSupportFragmentManager 
     switch(position) { 
      default: 
      case 0: 
       fragment = new SocialHistoryActivity(); 
       break; 
      case 1: 
       fragment = new EncounterActivity(); 
       break; 
     } 
     if (fragment != null) { 
      // update the main content by replacing fragments 
      FragmentManager fragmentManager = getSupportFragmentManager(); 
      fragmentManager.beginTransaction() 
        .replace(R.id.container, fragment) 
        .commit(); 
     } 
    } 

    public void onSectionAttached(int number) { 
     switch (number) { 
      case 1: 
       mTitle = getString(R.string.social_history); 
       break; 
      case 2: 
       mTitle = getString(R.string.encounter); 
       break; 
     } 
    } 

    public void restoreActionBar() { 
     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
     actionBar.setDisplayShowTitleEnabled(true); 
     actionBar.setTitle(mTitle); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     if (!mNavigationDrawerFragment.isDrawerOpen()) { 
      restoreActionBar(); 
      return true; 
     } 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     int id = item.getItemId(); 
     return super.onOptionsItemSelected(item); 
    } 

    public static class PlaceholderFragment extends Fragment { 

     private static final String ARG_SECTION_NUMBER = "section_number"; 

     public static PlaceholderFragment newInstance(int sectionNumber) { 
      PlaceholderFragment fragment = new PlaceholderFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     public PlaceholderFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
      return rootView; 
     } 

     @Override 
     public void onAttach(Activity activity) { 
      super.onAttach(activity); 
      ((AndroidNavDrawerActivity) activity).onSectionAttached(
        getArguments().getInt(ARG_SECTION_NUMBER)); 
     } 
    } 
} 

このNavigationDrawerFragment.java

public class NavigationDrawerFragment extends Fragment { 


    private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position"; 
    private NavigationDrawerCallbacks mCallbacks; 
    private ActionBarDrawerToggle mDrawerToggle; 

    private DrawerLayout mDrawerLayout; 
    private ListView mDrawerListView; 
    private View mFragmentContainerView; 
    private int mCurrentSelectedPosition = 0; 

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

     if (savedInstanceState != null) { 
      mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION); 
     } 


     // Select the default item. 
     selectItem(mCurrentSelectedPosition); 
    } 

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

     // the fragment has menu items to contribute 
     setHasOptionsMenu(true); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     mDrawerListView = (ListView) inflater.inflate(
       R.layout.fragment_navigation_drawer, container, false); 
     mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView parent, View view, int position, long id) { 
       selectItem(position); 
      } 
     }); 
     mDrawerListView.setAdapter(new ArrayAdapter(
       getActionBar().getThemedContext(), 
       android.R.layout.simple_list_item_activated_1, 
       android.R.id.text1, 
       new String[]{ 
         getString(R.string.social_history), 
         getString(R.string.encounter) 
       })); 
     mDrawerListView.setItemChecked(mCurrentSelectedPosition, true); 
     return mDrawerListView; 
    } 

    private ActionBar getActionBar() { 
     return ((AppCompatActivity) getActivity()).getSupportActionBar(); 
    } 

    public boolean isDrawerOpen() { 
     return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView); 
    } 


    public void setUp(int fragmentId, DrawerLayout drawerLayout) { 
     mFragmentContainerView = getActivity().findViewById(fragmentId); 
     mDrawerLayout = drawerLayout; 
     // set up the drawer's list view with items and click listener 
     ActionBar actionBar = getActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setHomeButtonEnabled(true); 
     // ActionBarDrawerToggle ties together the the proper interactions 
     // between the navigation drawer and the action bar app icon. 
     mDrawerToggle = new ActionBarDrawerToggle(
       getActivity(), 
       mDrawerLayout, 
       null, 
       R.string.navigation_drawer_open, 
       R.string.navigation_drawer_close 
     ) { 
      @Override 
      public void onDrawerClosed(View drawerView) { 
       super.onDrawerClosed(drawerView); 
       if (!isAdded()) { 
        return; 
       } 
       getActivity().supportInvalidateOptionsMenu(); 
      } 

      @Override 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       if (!isAdded()) { 
        return; 
       } 
       getActivity().supportInvalidateOptionsMenu(); 
      } 
     }; 


     mDrawerLayout.post(new Runnable() { 
      @Override 
      public void run() { 
       mDrawerToggle.syncState(); 
      } 
     }); 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 
     mDrawerToggle.setDrawerIndicatorEnabled(true); 
    } 

    private void selectItem(int position) { 
     mCurrentSelectedPosition = position; 
     if (mDrawerListView != null) { 
      mDrawerListView.setItemChecked(position, true); 
     } 
     if (mDrawerLayout != null) { 
      mDrawerLayout.closeDrawer(mFragmentContainerView); 
     } 
     if (mCallbacks != null) { 
      mCallbacks.onNavigationDrawerItemSelected(position); 
     } 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     try { 
      mCallbacks = (NavigationDrawerCallbacks) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException("Activity must implement NavigationDrawerCallbacks."); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mCallbacks = null; 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    public static interface NavigationDrawerCallbacks { 
     void onNavigationDrawerItemSelected(int position); 
    } 
} 

fragment_main.xml

<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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".activity.AndroidNavDrawerActivity$PlaceholderFragment"> 

    <TextView 
     android:id="@+id/section_label" 
     android:layout_width="240dp" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

答えて

0

は、まず第一に、ツールバーのレイアウトを作成している:toolbar.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    android:layout_height="56dp" 
    android:layout_width="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:background="@color/colorPrimary" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:gravity="center_vertical" 
     >  

     <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Login" 
    android:textSize="18sp" 
    android:gravity="center|center_vertical" 
    android:layout_weight="1" 
    android:textColor="@android:color/white" 
    android:textStyle="bold" 
    android:visibility="visible" 
    android:id="@+id/toolbar_title" />  

</LinearLayout> 
</android.support.v7.widget.Toolbar> 

はそれを含めますあなたがしたい活動se:

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

1)あなたのbuild.gradleに依存関係(アプリケーションレベル) -

compile 'com.android.support:appcompat-v7:23.x.x' 
compile 'com.android.support:design:23.x.x' 

2)あなたのXMLファイルでは、追加 -

<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" 
      app:title="Hello" 
      app:titleTextColor="#FF0000" 
      android:background="?attr/colorPrimary" /> 

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

3)あなたの活動ののonCreate()メソッドは、以下の行を追加 -

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 
をあなたがツールバーたい場合はUPDATE

すべてのアクティビティがそれぞれのXMLファイルにそのXMLパーツを追加し、それぞれのアクティビティにJAVAパーツを追加します。

この場合、「activity_main」に2番目のポイント(XML部分)を追加し、「AndroidNavDrawerActivity」に3番目のポイント(JAVA部分)を追加することができます。

これはツールバーをActionBarとして追加します。希望すると助けてくれるでしょう:)

+0

私は分かりません。私はこのコードを挿入することができる第3、ポイント、? AndroidNavDrawerActivityまたはNavigationDrawerFragmentでは – bircastri

+0

どのアクティビティでツールバーを使用したいですか? –

+0

すべてのフラグメント(アクティビティではありません)では、コードはどのように動作するのか分かりません。 ファイルxmlでは、コードを挿入できますか? フラグメント内で、そのコードを挿入してツールバーを読み込むことはできますか? – bircastri

関連する問題