2017-08-05 124 views
-1

我是初學者到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

使用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>  
+0

謝謝。現在它工作了 – user3661367