我是Android的新用户,在我的应用中,我正在使用DrawerLayout
,这是
好的很好。但我想更改ActionBarDrawerToggle
图标。
我们如何改变它?
我尝试了很多,但我无法达到预期的效果,请帮帮我一个人吗?
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/navigation_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</LinearLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
public class MainActivity extends AppCompatActivity {
ActionBarDrawerToggle actionBarDrawerToggle;
DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
}
答案 0 :(得分:6)
将此行添加到工具栏
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
ActionBarDrawerToggle mToogle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
drawerLayout.addDrawerListener(mToogle);
mToogle.syncState();
toolbar.setNavigationIcon(R.drawable.ic_your_image);
mToogle.syncState()将处理操作(打开和关闭导航抽屉)。
答案 1 :(得分:1)
你必须完成3个步骤:
actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
toolbar.setNavigationIcon(R.drawable.menu_icon);
将OnClick添加到新图标,如下所示:
toolbar.setNavigationOnClickListener(new View.OnClickListener(){ @覆盖 public void onClick(查看v){ drawerLayout.openDrawer(Gravity.LEFT); } });
答案 2 :(得分:-1)
可能是正确的:-P
public class MainActivity extends AppCompatActivity {
ActionBarDrawerToggle actionBarDrawerToggle;
DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer);
drawerLayout = new ActionBarDrawerToggle(
this, /* host Activity */
drawerLayout , /* DrawerLayout object */
R.drawable.drawer_icon, /* nav drawer icon to replace 'Up' caret */
"drawer_open", /* "open drawer" description */
"drawer_close" /* "close drawer" description */
)
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
}