Android: 我需要设置 Drawer "burger icon" onClickListener 吗?
Android: do i need to setup Drawer "burger icon" onClickListener?
我正在尝试设置带抽屉和 toolbar/action 栏杆的 activity,但我在设置时遇到了一些问题。
我的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"/>
Activity:
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.........
}
我希望能够在不使用 XML 配置(主题)的情况下更改背景颜色、文本颜色和图标(导航汉堡或任何其他图标)。
据我所知,setSupportActionBar(toolbar) 会为我实现 clickListener。但是当我不指定监听器时,按钮根本不起作用(根本没有任何动作)。
通常您会使用抽屉侦听器来侦听抽屉的 open/close 事件。创建此侦听器时,您可以指定工具栏,然后您的按钮将起作用。
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
对象。
这是(大部分)在您创建带抽屉的 activity 时在 AndroidStudio 中默认生成的代码,因此我认为这是最佳实践。
我正在尝试设置带抽屉和 toolbar/action 栏杆的 activity,但我在设置时遇到了一些问题。
我的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"/>
Activity:
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.........
}
我希望能够在不使用 XML 配置(主题)的情况下更改背景颜色、文本颜色和图标(导航汉堡或任何其他图标)。
据我所知,setSupportActionBar(toolbar) 会为我实现 clickListener。但是当我不指定监听器时,按钮根本不起作用(根本没有任何动作)。
通常您会使用抽屉侦听器来侦听抽屉的 open/close 事件。创建此侦听器时,您可以指定工具栏,然后您的按钮将起作用。
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
对象。
这是(大部分)在您创建带抽屉的 activity 时在 AndroidStudio 中默认生成的代码,因此我认为这是最佳实践。