[英]Hamburger icon on Toolbar acts as back button
我嘗試使用 androidx 庫在我的應用程序中實現導航抽屜。 到目前為止,該應用程序在左上角顯示漢堡包圖標,但要訪問抽屜,我必須向右滑動。 如果我單擊該圖標,它將轉到上一個活動。 是不是因為這個活動不是主要活動? 我怎樣才能解決這個問題? 先感謝您。
這是我的活動
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
DrawerLayout drawerLayout = findViewById(R.id.drawerLayout);
ActionBarDrawerToggle t = new ActionBarDrawerToggle(this, drawerLayout,R.string.Open, R.string.Close);
drawerLayout.addDrawerListener(t);
t.syncState();
NavigationView nv = findViewById(R.id.navigationView);
nv.setNavigationItemSelectedListener(item -> {
int id = item.getItemId();
switch(id)
{
case R.id.action_open_list:
break;
case R.id.action_closed_list:
Intent closedListIntent = new Intent(this, ClosedListActivity.class);
startActivity(closedListIntent);
break;
default:
return true;
}
return true;
});
}
我的布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/tv_error_message_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/error_message_cannot_connect"
android:textSize="20sp"
android:visibility="invisible" />
<ProgressBar
android:id="@+id/pb_loading_indicator"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_gravity="center"
android:visibility="invisible" />
</FrameLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
android:id="@+id/navigationView"/>
</androidx.drawerlayout.widget.DrawerLayout>
Toolbar
需要是DrawerLayout
的子DrawerLayout
才能使用。
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/tv_error_message_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/error_message_cannot_connect"
android:textSize="20sp"
android:visibility="invisible" />
<ProgressBar
android:id="@+id/pb_loading_indicator"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_gravity="center"
android:visibility="invisible" />
</FrameLayout>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
android:id="@+id/navigationView"/>
</androidx.drawerlayout.widget.DrawerLayout>
然后在你的活動中
@Override
protected void onCreate(Bundle savedInstanceState) {
....
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id. drawerLayout);
ActionBarDrawerToggle t = new ActionBarDrawerToggle(
this, drawerLayout, toolbar, R.string.Open, R.string.Close);
drawerLayout.addDrawerListener(t);
t.syncState();
....
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.