android - 如何添加 NavigationView 抽屉 Android 的图标
问题描述
我是一名新的应用程序开发人员。我做了一个(NavigationView 抽屉)的例子,效果很好。但我的问题是我需要知道如何添加它。
像这样:
我已在活动 bar_main 中将其添加为 ImageButton,但我不需要那样。因此,我有以下问题:
我见过很多例子,图标不是以这种方式添加的,它不是 ImageButton。这就像来自没有任何 ImageButton 的系统。所以我需要这样做。
现在,如果我这样工作,它将与搜索图标重叠。按下搜索按钮时,抽屉图标应该消失。
我的代码:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
static ImageButton menuRight;
private DrawerLayout drawer;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_m);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
navigationView.setNavigationItemSelectedListener(this);
navigationView.bringToFront();
toolbar =findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
menuRight =(ImageButton) findViewById(R.id.leftRight);
menuRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
});
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
drawer.closeDrawers();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
int id = item.getItemId();
if (id == R.id.nav_home) {
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
} else if (id == R.id.nav_Politics) {
Intent i = new Intent(this, Ploysity.class);
startActivity(i);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
return false;
}
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
super.onBackPressed();
}
}
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"></androidx.appcompat.widget.Toolbar>
<ImageButton
android:id="@+id/leftRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimary"
android:src="@drawable/ic_menu_black_24dp" />
</RelativeLayout>
</com.google.android.material.appbar.AppBarLayout>
解决方案
如果你想改变图标,那么你需要添加这一行:
drawer.setHomeAsUpIndicator(R.drawable.ic_your_drawer_icon); //set your own
推荐阅读
- regex - 正则表达式:如何要求某个字符后面只能跟一个或多个数字
- css - Font Awesome 5 不显示所有 unicode
- javascript - 如何禁用 VSCode 函数/对象工具提示
- mysql - MySQL 服务器已消失 错误编号:2006
- swiftui - SwiftUI - 自定义导航“返回”按钮,奇怪地在视图之间来回跳跃
- python - Pandas KeyError:'发生在索引 0'
- html - 如何使用媒体查询减少 html 或正文宽度
- c# - 带有 EF Core 的 ASP.NET Core 注册页面上的错误
- ios - 将 SwiftUI 按钮绑定到 AnySubscriber,例如 RxCocoa 的按钮点击
- html - 重新排列分类显示页面中显示的专辑顺序