在导航抽屉布局中显示网格布局?

时间:2017-08-05 16:14:43

标签: android android-navigation-drawer

我是android的初学者。我想显示一个带有四个按钮和导航抽屉菜单的主页。我使用GridLayout来显示四个按钮。我不知道将GridLayout代码放在activity_main.xml中的哪个位置。我是否想使用LinearLayout或做什么?请帮帮我。

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="wrap_content"
        android:layout_height="200dp" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
    <GridLayout
        android:id="@+id/GridLayout2"
        android:layout_marginTop="200dp"
        android:layout_width="360dp"
        android:layout_height="500dp"
        android:columnCount="2"
        android:rowCount="2"
        android:orientation="horizontal">

        <Button
            android:id="@+id/orgnicshopbutton"
            android:text="@string/orgnanicshops" />

        <Button
            android:id="@+id/newseedsbutton"
            android:text="@string/new_seeds" />

        <Button
            android:id="@+id/tobenotedbutton"
            android:text="@string/to_be_noted" />

        <Button
            android:id="@+id/contactbutton"
            android:text="@string/contact" />

    </GridLayout>
</android.support.v4.widget.DrawerLayout>

</LinearLayout>

我在MainAcitvity.java中附加了所有按钮和GridLayout。

MainActivity.java

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    Button organicbtn,newseedsbtn,tobenotedbtn,contactsbtn;
    DrawerLayout drawer;
    ActionBarDrawerToggle toggle;
    GridLayout gridLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       organicbtn=(Button)findViewById(R.id.orgnicshopbutton);
        newseedsbtn=(Button)findViewById(R.id.newseedsbutton);
        tobenotedbtn=(Button)findViewById(R.id.tobenotedbutton);
        contactsbtn=(Button)findViewById(R.id.contactbutton);
        gridLayout=(GridLayout)findViewById(R.id.GridLayout2);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

1 个答案:

答案 0 :(得分:1)

在android文档中查看Creating a NavigationDrawerDrawerLayout应该有两个子视图。 第一个用于屏幕的常规内容 - 在这里您应该放置按钮。 第二个用于导航抽屉。

在伪xml中,它看起来像这样:

<DrawerLayout>
  <!-- main content -->
  <GridLayout>
    <Button>
    <Button>
    ...
  </GridLayout>
  <!-- this goes into the navigation drawer -->
  <LinearLayout>
    <Button> <!-- nav button -->
    <Button> <!-- another nav button -->
  </LinearLayout>
</DrawerLayout>