我是
Android的新手,在我的应用程序中,我使用的是DrawerLayout,它是
好的很好.但我想改变ActionBarDrawerToggle图标.
好的很好.但我想改变ActionBarDrawerToggle图标.
我们怎样才能改变它?
我尝试了很多,但我无法达到预期的效果,请帮助我一个人?
XML: –
<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); }
解决方法
将此行添加到工具栏
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,R.string.app_name); drawerLayout.addDrawerListener(mToogle); mToogle.syncState(); toolbar.setNavigationIcon(R.drawable.ic_your_image);
mToogle.syncState()将处理操作(打开和关闭导航抽屉).