我是android开发新手。我已经建立了我的应用程序,无需导航抽屉。现在,我想添加导航抽屉,它将在我的应用中的所有活动中保持一致。在这个问题上帮助我。如何在预建应用程序上添加导航抽屉?
0
A
回答
0
假设您的原始活动扩展了'AppCompatActivity',请将基本活动设置为如下并将其他所有活动扩展到'BaseActivity'。 BaseActivity布局将内容DrawaerLayout和一个framelayout。在BaseActivity中,使用setContentview方法,并在framelayout中填充活动布局。
BaseActivity.Java
public class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private FrameLayout baseLayout;
public ActionBarDrawerToggle drawerToggle;
public Toolbar toolbar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.base_layout);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
baseLayout = (FrameLayout) findViewById(R.id.base_view);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView.setNavigationItemSelectedListener(this);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, 0, 0);
drawerLayout.addDrawerListener(drawerToggle);
}
@Override
public void setContentView(View view) {
if (baseLayout != null) {
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
baseLayout.addView(view, params);
}
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
if (baseLayout != null) {
baseLayout.addView(view, params);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//TODO
return false;
}
}
base_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
style="@style/Widget.MyApp.Toolbar.Solid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/base_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="280sp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/base_header"
app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>
base_header.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="@dimen/nav_header_height"
android:background="@drawable/blank"
android:orientation="vertical">
<ImageView
android:id="@+id/nav_header_icon"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_marginBottom="8dp"
android:layout_marginStart="16sp"
android:contentDescription="@null"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toTopOf="@+id/nav_header_title"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/nav_header_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16sp"
android:layout_marginStart="16sp"
android:fontFamily="sans-serif-medium"
android:text="@string/app_name"
app:layout_constraintBottom_toTopOf="@+id/nav_header_subtitle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/nav_header_subtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16sp"
android:layout_marginStart="16sp"
android:fontFamily="sans-serif"
android:text="@string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
在 “菜单中的” 添加文件夹drawer.xml在你的资源。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/icon_home"
android:title="Home" />
</menu>
相关问题
- 1. 在片段上添加导航抽屉
- 2. 如何在使用导航抽屉的片段时在底部导航栏上添加导航抽屉?
- 3. Web应用程序的导航抽屉
- 4. 如何使用反应导航在导航抽屉中添加节分隔线
- 5. 在片段上创建导航抽屉
- 6. 动态添加MDL导航抽屉
- 7. 添加按钮,导航抽屉片段
- 8. 导航抽屉如何添加上述列表视图
- 9. 创建导航抽屉
- 10. Android Studio导航抽屉,如Gmail应用
- 11. 将导航抽屉添加到Android Studio中的我的应用程序
- 12. 导航抽屉:fragmentTransaction.commit()正在崩溃我的应用程序
- 13. 导航抽屉在应用程序启动时打开
- 14. 无法在应用程序启动时关闭导航抽屉
- 15. 如何在导航抽屉中将项目添加为项目
- 16. 如何在导航抽屉活动中添加Google地图?
- 17. 如何在导航抽屉中添加页脚?
- 18. Android导航抽屉?
- 19. 如何在导航中抽屉
- 20. 在导航抽屉中导航错误?
- 21. 用NativeScript导航抽屉
- 22. 添加导航抽屉通过替换在导航栏中的导航栏
- 23. 导航抽屉 - 网络应用
- 24. 导航抽屉与导航抽屉片段
- 25. 导航抽屉QR扫描器应用程序android studio
- 26. 当我启动导航抽屉时,应用程序崩溃
- 27. 我的应用程序崩溃与导航抽屉
- 28. 导航抽屉中的片段布局Android应用程序
- 29. 导航抽屉应用程序崩溃API 10
- 30. 如何添加向右滑动以打开导航抽屉
最简单的方法将是使用带有NavigationDrawer只有一个活动并使用不同的内容片断(而不是有许多不同的活动的老办法)... – deHaar