2015-06-11 96 views
0

我試圖用抽屜和工具欄/動作欄設置一個活動,但即時通訊設置有一些問題。Android:我需要安裝抽屜「漢堡圖標」onClickListener嗎?

main_activity.xml

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/DrawerLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:elevation="7dp」> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <include 
     android:id="@+id/main_tool_bar" 
     layout="@layout/toolbar" /> 

    <FrameLayout 
     android:id="@+id/main_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/main_tool_bar"/> 
</RelativeLayout> 
<!--... the rest of my layout for the drawer...--> 

我toolbar.xml:

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/toolbar_color"/> 

活動:

public class MainActivity extends ActionBarActivity { 

private void setupToolbar() { 
     Toolbar toolbar = (Toolbar) findViewById(R.id.main_tool_bar); 
     setSupportActionBar(toolbar); 
     toolbar.setNavigationIcon(R.drawable.ic_menu_white); 
     toolbar.setTitle(getResources().getString(R.string.to_sign)); 

     toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mDrawerLayout.openDrawer(Gravity.LEFT); 
      } 
     }); 

     setActionBarTextColor(Color.WHITE); 
} 


    //........ more code not related with bar or drawer......... 

} 

我希望能夠改變背景顏色,文字顏色和圖標(導航漢堡,或任何其他圖標),而不使用X. ML配置(主題)。

據我所知,setSupportActionBar(toolbar)會爲我實現clickListener。但是,當我不指定偵聽器時,該按鈕根本不起作用(根本沒有動作)。

+0

」有太多可能的答案,或者對於這種格式來說答案太好,請添加詳細信息以縮小答案集或隔離幾個段落中可以回答的問題。「 – Simas

+0

@Simas,我只是更新我的問題。你是對的,當時有一個問題對每個人都更清楚。 – Bugdr0id

回答

1

通常情況下,您可以使用抽屜偵聽器來聆聽抽屜的打開/關閉事件。創建此偵聽器時,您可以指定工具欄,然後您的按鈕將工作。

public void setUp(int fragmentId, DrawerLayout drawerLayout) { 
    ... 
    mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, getToolbar(), 
      R.string.navigation_drawer_open, R.string.navigation_drawer_close) { 

     @Override 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
     } 

     @Override 
     public void onDrawerClosed(View drawerView) { 
      super.onDrawerClosed(drawerView); 
     } 

    }; 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    // Forward the new configuration the drawer toggle component. 
    mDrawerToggle.onConfigurationChanged(newConfig); 
} 

當然你可以選擇不覆蓋的方法,只是使用普通ActionBarDrawerToggle對象。

這是(大部分)在AndroidStudio中默認生成的代碼,當您使用抽屜創建活動時,我認爲這是最佳做法。 「

相關問題