需使用@android:style/Theme.Holo.Light.DarkActionBar 開啟Action Bar
主layout.xml需改為<android.support.v4.widget.DrawerLayout>
裏面包含FrameLayout及ListView, 再使用setContentView(R.layout.xxx)即可看到由左往右拖曳的效果
List清單 : 完成如下作動,拖曳出來就有清單
新增一個TextView之Layout, 方便下面adapter的顯示
新增ArrayAdapter : ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, R.layout.drawer_list_item, planetTitles);
drawList.setAdapter(adapter);
活化ActionBar
getActionBar().setDisplayHomeAsUpEnabled(true);
@Override
public boolean onOptionsItemSelected(MenuItem item){
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}
else{
drawerLayout.openDrawer(GravityCompat.START);
}
return super.onOptionsItemSelected(item);
}
變更ActionBar標題
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(
this, /* 主要 Activity */
drawerLayout, /* DrawerLayout xml */
0,0
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
}
};
// ActionBarDrawerToggle 狀態與 DrawerLayout 同步
drawerToggle.syncState();
// DrawerLayout設定DrawerToggle監聽器
drawerLayout.setDrawerListener(drawerToggle);
變更圖片-content_frame
新增fragmentlayout.xml, 安排ImageView
新增ImageFragment class, 將layout塞入
drawList新增監聽
drawList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
設定Bundle, 將position傳入
ft.replace(R.id.content_frame, fragment)
drawList.setChecked(position, true);
drawLayout.closeDrawer(GravityCompat.START)
}
};