简体   繁体   English

如何在预建的应用程序上添加导航抽屉?

[英]How to add navigation drawer on pre built app?

I am new to android development. 我是android开发的新手。 I have already built my app without navigation drawer. 我已经构建了没有导航抽屉的应用程序。 Now, I want to add navigation drawer which will be same across all activities in my app. 现在,我想添加导航抽屉,该抽屉在我的应用程序中的所有活动中都相同。 Help me on this issue. 帮助我解决这个问题。

Assuming your original activities are extending 'AppCompatActivity', make base activity like following and extend your all other activities to 'BaseActivity'. 假设您的原始活动正在扩展“ AppCompatActivity”,请像下面那样进行基本活动,并将所有其他活动扩展到“ BaseActivity”。 BaseActivity layout will content DrawaerLayout and one framelayout. BaseActivity布局将包含DrawaerLayout和一个framelayout。 Inside BaseActivity ovveride setcontentview method and inflat your activity layout inside framelayout. 在BaseActivity ovveride setcontentview方法内部,并在framelayout内部扩大活动布局。

BaseActivity.Java 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 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 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>

Add drawer.xml in "menu" folder in your resources. 在资源中的“ menu”文件夹中添加抽屉。

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM