嗨,我正在使用"android.support.v4.widget.DrawerLayout"抽屉.我想改变抽屉图标的颜色.请告诉我如何做到这一点.我分享我的代码.请看一下并建议我如何更改图标颜色更改.
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationIcon(R.drawable.drawer);
toolbar.hideOverflowMenu();
getSupportActionBar().setTitle(null);
ImageView img = (ImageView) findViewById(R.id.imgCourses);
mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
mRecyclerView.setHasFixedSize(true);
Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);
mAdapter = new MyAdapter(TITLES,NAME,EMAIL,PROFILE,this);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setVerticalScrollBarEnabled(false);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mDrawerToggle = new ActionBarDrawerToggle(this,
Drawer,
toolbar,
R.string.openDrawer,
R.string.closeDrawer)
{
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
Drawer.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
Drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
Run Code Online (Sandbox Code Playgroud) 这是我的mainlayout文件。
这是主布局文件,我在其中编写与导航抽屉相关的代码,还使用 include 包含工具栏。
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v4.widget.DrawerLayout
android:id="@+id/navigation_Drawerlayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include layout="@layout/navigation_drawerlayout" />
<android.support.design.widget.NavigationView
android:id="@+id/navigation_View"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/navigation_headerview" />
<android.support.v7.widget.RecyclerView
android:id="@+id/navigationRecycleView"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
navigation_toolbarlayout.xml
该布局定义了toolbar上面代码中使用的代码
<?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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:titleTextColor="@android:color/white">
<!-- Screen title -->
<TextView
android:id="@+id/toolbarTitle"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=""
android:textColor="@android:color/white"></TextView>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout> …Run Code Online (Sandbox Code Playgroud) 我通过在GitHub的一个简单测试项目中添加工具栏和右侧抽屉,扩展了着名的Google 导航抽屉示例:

如何在工具栏中添加"汉堡"按钮 - 这将打开正确的抽屉(目前只能用手指轻扫)?
左侧抽屉的"汉堡"按钮已经存在并运行良好(可能是由ActionBarDrawerToggle添加的?)
这是我当前的activity_main.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice" />
<ListView
android:id="@+id/right_drawer"
android:layout_width="160dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ListView mActionList;
private ActionBarDrawerToggle mDrawerToggle;
private String[] mPlanetTitles;
private String[] mActions;
private int[] …Run Code Online (Sandbox Code Playgroud) android android-layout material-design android-toolbar android-drawer
我从应用程序中删除了操作栏,所以我需要创建按钮来自己打开抽屉 Activity 上的按钮工作正常但是当我在片段应用程序中使用它时停止工作(当我调用片段时)
我在 Activity 中使用的代码(工作正常的代码)
ImageButton btn = (ImageButton)findViewById(R.id.drawerButton);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.openDrawer(Gravity.START);
}
});
Run Code Online (Sandbox Code Playgroud)
fragment_achievements.xml
<android.support.constraint.ConstraintLayout 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"
tools:context="layout.AchievementsFragment">
<ImageButton
android:id="@+id/imageButton"
android:layout_width="28dp"
android:layout_height="29dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_menu_button" />
<WebView
android:id="@+id/web_ach"
android:layout_width="409dp"
android:layout_height="602dp"
android:layout_marginBottom="-9dp"
android:layout_marginLeft="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
tools:layout_editor_absoluteY="56dp" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
成就片段.java
package layout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView; …Run Code Online (Sandbox Code Playgroud) android ×4