gpt4 book ai didi

android - 更改工具栏中的 ActionBarDrawerToggle 图标 android?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:40 25 4
gpt4 key购买 nike

我有一个带有抽屉导航和工具栏的 Activity

Activity

public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private CustomTextViewMondaRegular tvTitle;
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);

mDrawerList = (ListView) findViewById(R.id.left_drawer);
tvTitle = (CustomTextViewMondaRegular) findViewById(R.id.actionTitle);
tvTitle.setText(getIntent().getStringExtra("title"));
mDrawerList.setAdapter(new NavAdapter(getApplicationContext()));
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,toolbar,
R.string.title_activity_main, R.string.title_activity_main) {
@Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}

@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
// creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {

}
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}

public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
Logger.e("pos", position + "");
}
}

Activity XML

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.vf.MainActivity">

<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">



<android.support.v7.widget.Toolbar
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/white"
android:elevation="3dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/MyDarkToolbarStyle">

<TextView
android:id="@+id/actionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:drawableEnd="@drawable/logo"
android:drawableRight="@drawable/logo"
android:gravity="center"
android:text="XXXXXX"
android:textColor="#000000"
android:textSize="18sp" />

</android.support.v7.widget.Toolbar>
</FrameLayout>

<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:choiceMode="singleChoice"
android:divider="#D8D8D8" />

切换图标当前显示默认图标。我需要更改该图标。我该怎么做?

最佳答案

对于将来遇到这个 SO 问题的人,

我们必须禁用抽屉指示器

mDrawerToggle.setDrawerIndicatorEnabled(false);

然后设置ToolBar的导航按钮

 mToolbar.setNavigationIcon(R.drawable.navIcon);

附言之后我们必须在工具栏上设置导航点击监听器并手动打开NavigationDrawer。喜欢mToolbar.setNavigationOnClickListener :D

关于android - 更改工具栏中的 ActionBarDrawerToggle 图标 android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31238396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com