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

时间:2017-07-28 11:00:08

标签: java android android-activity navigation-drawer

我是Android开发的新手。我已经构建了我的应用程序没有导航抽屉。现在,我想添加导航抽屉,它在我的应用程序中的所有活动中都是相同的。在这个问题上帮助我。

1 个答案:

答案 0 :(得分:0)

假设您的原始活动正在扩展' AppCompatActivity',请进行基本活动,例如关注并将您的所有其他活动扩展到' BaseActivity'。 BaseActivity布局将内容DrawaerLayout和一个framelayout。在BaseActivity内部使用setcontentview方法并在framelayout中填充你的活动布局。

BaseActivity.Java

np.random.seed(45)
df = pd.DataFrame(np.random.randint(10, size=(10,5)))
print (df)
   0  1  2  3  4
0  3  0  5  3  4
1  9  8  1  5  9
2  6  8  7  8  5
3  2  8  1  6  4
4  8  4  6  4  9
5  1  6  8  8  1
6  6  0  4  9  8
7  0  9  2  6  7
8  0  0  2  9  2
9  6  0  9  6  0

N = 3
df = df.iloc[np.r_[0:N, -N:0]]
print (df)
   0  1  2  3  4
0  3  0  5  3  4
1  9  8  1  5  9
2  6  8  7  8  5
7  0  9  2  6  7
8  0  0  2  9  2
9  6  0  9  6  0

base_layout.xml

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_header.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>

在&#34;菜单&#34;中添加drawer.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>