4
所以我試圖實現一個使用appcompat庫的導航抽屜。我正在使用工具欄作爲我的操作欄。我的問題是我的工具欄正在填滿整個屏幕。Android工具欄填滿整個屏幕
這裏是我的工具欄。
<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發生在除了導航抽屜不再打開。
固定部分問題!我試圖用DrawerLayout內部和外部的工具欄。在它裏面,行爲沒有區別。但在外面,導航抽屜現在將打開。然而,我仍然有[這個問題](http://imgur.com/x41kLQi)。有什麼建議麼? – ColinMKH 2015-02-23 21:05:29
@ColinMKH更新了答案。 – 2015-02-23 21:26:26
完美的作品!謝謝! – ColinMKH 2015-02-23 21:31:57