2015-02-23 151 views
4

所以我试图实现一个使用appcompat库的导航抽屉。我正在使用工具栏作为我的操作栏。我的问题是我的工具栏正在填满整个屏幕。Android工具栏填满整个屏幕

This being the problem

这里是我的工具栏。

<android.support.v7.widget.Toolbar 
    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="wrap_content" 
    android:id="@+id/toolbar" 
    android:minHeight="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" /> 

而我的主要活动。

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools"  
     android:layout_width="match_parent" 
     android:layout_height="match_parent" tools:context=".MainActivity"> 



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

     <include layout="@layout/toolbar_main"/> 

     <!--- Main Layout --> 
     <FrameLayout 
      android:id="@+id/content_frame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
     <!--- Nav Drawer --> 
     <ListView 
      android:id="@+id/navigation_drawer" 
      android:layout_width="@dimen/drawer_width" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:choiceMode="singleChoice" 
      android:divider="@android:color/transparent" 
      android:dividerHeight="0dp" 
      android:background="#FFF"/> 
    </android.support.v4.widget.DrawerLayout> 

</LinearLayout> 

最后我的来源。

public class MainActivity extends ActionBarActivity { 

    private Toolbar mToolbar; 
    private ActionBarDrawerToggle mDrawerToggle; 
    private DrawerLayout mDrawerLayout; 
    private CharSequence mTitle; 
    private ListView mDrawerList; 
    private String[] mAddresses; 
    private CharSequence mDrawerTitle; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mAddresses = getResources().getStringArray(R.array.addresses); 
     mToolbar = (Toolbar) findViewById(R.id.toolbar); 
     //mToolbar.setNavigationIcon(R.drawable.ic_myaccount); 

     setSupportActionBar(mToolbar); 
     mTitle = mDrawerTitle = getTitle(); 
     //Set up the nav drawer 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,mToolbar,R.string.open_drawer,R.string.close_drawer); 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 


     //Drawer List 
     mDrawerList = (ListView) findViewById(R.id.navigation_drawer); 
     mDrawerList.setAdapter(new ArrayAdapter<>(this, R.layout.drawer_list_item, mAddresses)); 
     mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

     getSupportActionBar().setHomeButtonEnabled(true); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 

     mDrawerToggle.syncState(); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState){ 
     super.onPostCreate(savedInstanceState); 
     mDrawerToggle.syncState(); 
    } 
} 

编辑:

如果我移动工具栏包括DrawerLayout之外,则this发生在除了导航抽屉不再打开。

回答

12

对于第一个问题:

您正在使用LinearLayout。默认方向为水平。

您应该将android:orientation="vertical"添加到您的根元素。

对于第二个问题:

删除android:fitsSystemWindows="true"或移动工具栏在DrawerLayout第一元件的内部。 注意DrawerLayout里面必须有2个视图。

<LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" tools:context=".MainActivity" 
      android:orientation="vertical"> 

     <android.support.v4.widget.DrawerLayout> 

     <!--- Main Layout --> 
     <LinearLayout android:orientation="vertical"> 

      <include layout="@layout/toolbar_main"/> 

      <FrameLayout 
      android:id="@+id/content_frame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 



     </LinearLayout> 
     <ListView /> 

     </android.support.v4.widget.DrawerLayout> 
    </LinearLayout> 
+0

固定部分问题!我试图用DrawerLayout内部和外部的工具栏。在它里面,行为没有区别。但在外面,导航抽屉现在将打开。然而,我仍然有[这个问题](http://imgur.com/x41kLQi)。有什么建议么? – ColinMKH 2015-02-23 21:05:29

+0

@ColinMKH更新了答案。 – 2015-02-23 21:26:26

+0

完美的作品!谢谢! – ColinMKH 2015-02-23 21:31:57