我想使侧面导航栏与启用backpress的playotre一样。 与在Goggle Play商店应用中一样,打开应用时会显示HomeFragment /主屏幕,但侧面导航栏中未显示/提及HomeFragment。 我想要在我的应用中使用此东西。
而且,如果我们移至“我的应用”,则在此之后按“后退”按钮移至HomeFragment。
这是Play商店的HomeFragment /主屏幕。在红色圆圈中,您可以看到开放式导航栏上的Humburger图标。
在图片2中,我们可以看到它们没有显示或提及HomeFragment / Home屏幕。
答案 0 :(得分:1)
如果您的意思是Navigation View
,请看以下示例:
这是主要的xml
文件(要在其中放置布局的布局),它将是drawer layout
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:openDrawer="start"
android:id="@+id/drawer_layout">
a toolbar instead of action bar and the relative layout will just debug the bugs
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolBar"
android:background="#9C27B0"
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"/>
</RelativeLayout>
this is the navigation view
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="320dp"
android:layout_height="fill_parent"
android:layout_gravity="start"
app:menu="@menu/nav_view_menu"
app:headerLayout="@layout/nav_header"/>
</androidx.drawerlayout.widget.DrawerLayout>
tools:openDrawer="start"
代表菜单在左侧或右侧出现的位置,
nav header
是navigation_view
顶部的图片
,app:menu
是navigation_view
菜单
这是主要活动:
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.MenuItem;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout ;
NavigationView navigationView ;
ActionBarDrawerToggle actionBarDrawerToggle ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//this will make toolbar instead of action bar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar) ;
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout) ;
navigationView = (NavigationView) findViewById(R.id.nav_view) ;
// if user select item from the navigation view it will be detected here
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.item1 : System.out.println("item 1 selected") ; break;
case R.id.item2 : System.out.println("item 2 selected") ; break;
}
return true;
}
});
// this is important part to add button in the toolbar
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open_nav,R.string.close_nav) ;
actionBarDrawerToggle.syncState();
drawerLayout.setDrawerListener(actionBarDrawerToggle) ;
}
}
这是nav_header
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="170dp"
android:background="#4CAF50">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="this is nav header"
android:textSize="40sp" />
</RelativeLayout>
layout_height
是首选170
并且您必须创建一个像这样的菜单:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
you create a group and dont't forget to set checkable beavior to single and then
add your items
<group android:checkableBehavior="single">
<item android:id="@+id/item1" android:title="this is item1" />
<item android:id="@+id/item2" android:title="this is item2"/>
</group>
</menu>
tools:showIn="navigation_view
非常重要!
在此之前,您必须在implementation 'com.android.support:design:28.0.0'
文件的依赖项中添加build.gradle (moudle)
并将其添加到您的strings.xml
文件中:
<string name="open_nav">navigation_view_opened</string> <string name="close_nav">navigation_view_closed</string>
要隐藏action bar
(非常重要),请对您的styles.xml
执行此操作:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
_________________________________________________________________________________
代码结果: