pullList屬性使用效果圖示例:
android
//不用佈局ide
//MainActivity 類佈局
代碼this
public class MainActivity extends Activity {spa
@Override.net
protected void onCreate(Bundle savedInstanceState) {orm
super.onCreate(savedInstanceState);xml
setContentView(R.layout.activity_main);事件
//===============pullList的使用============================utf-8
ActionBar actionBar = getActionBar();
//設置ActionBar的類型
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
//添加須要的數據
final List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
Map<String, String> optionMap1 = new HashMap<String, String>();
optionMap1.put("option", "首頁");
Map<String, String> optionMap2 = new HashMap<String, String>();
optionMap2.put("option", "下頁");
Map<String, String> optionMap3 = new HashMap<String, String>();
optionMap3.put("option", "上頁");
dataList.add(optionMap1);
dataList.add(optionMap2);
dataList.add(optionMap3);
//自定義適配器
SimpleAdapter adapter = new SimpleAdapter(this, dataList, android.R.layout.simple_list_item_1, new String[]{"option"}, new int[]{android.R.id.text1});
//actionBar事件監聽
//onNavigationItemSelected方法監聽
actionBar.setListNavigationCallbacks(adapter, new OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Toast.makeText(MainActivity.this, "這是" + dataList.get(itemPosition).get("option"), 100).show();
return false;
}
});
}
}
===================================================
//須要實現的功能:在ActionBar欄中放一些Android系統自定義圖標
//其中有返回 -- 點擊返回 直接返回到原來的界面
//有個選擇菜單 選中彈出
效果圖示例:
//返回的箭頭圖標本身找個放入res/drawable下
一、建立一個菜單文件menu在res裏
菜單佈局 actionbar_menu.xml
代碼
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 系統自帶的圖標 -->
<item
android:id="@+id/actionbar_call"
android:icon="@android :drawable/ic_menu_call"
android:orderInCategory="1"
android:showAsAction="always"
android:title=""/>
<item
android:id="@+id/actionbar_camera"
android:icon="@android:drawable/ic_menu_camera"
android:orderInCategory="2"
android:showAsAction="always"
android:title=""/>
<item
android:id="@+id/actionbar_share"
android:icon="@android:drawable/ic_menu_share"
android:orderInCategory="3"
android:showAsAction="always"
android:title=""/>
<!-- 自定義的圖標 actionViewClass 引號裏面放的是要定義的控件類型的包名-->
<item
android:id="@+id/actionbar_more"
android:showAsAction="always"
android:actionViewClass="android.widget.ImageView"
android:title=""/>
</menu>
============================
二、MainActivity 類
代碼
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取ActionBar
ActionBar actionBar = getActionBar();
//設置自定義的圖標能夠點擊
actionBar.setHomeButtonEnabled(true);
actionBar.setIcon(R.drawable.ie_arrow_normal);
actionBar.setTitle("返回");
}
//一、建立一個選擇菜單
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbar_menu, menu);
//在下面設置自定義的圖標
MenuItem item_menu = menu.findItem(R.id.actionbar_more);//找到菜單佈局裏自定義的圖標名字
ImageView imageview = (ImageView) item_menu.getActionView();
imageview.setImageResource(R.drawable.skin_aio_more_nor);
//圖標的事件監聽
imageview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//建立一個popMenu彈出菜單
PopupMenu popMenu = new PopupMenu(MainActivity.this, v);
//設置菜單的選項
popMenu.getMenu().add(Menu.NONE, 1, Menu.NONE, "分享");
popMenu.getMenu().add(Menu.NONE, 2, Menu.NONE, "掃一掃");
popMenu.getMenu().add(Menu.NONE, 3, Menu.NONE, "搖一搖");
//菜單選項的事件監聽
popMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()){
case 1:
Toast.makeText(MainActivity.this, "分享", 100).show();
break;
case 2:
Toast.makeText(MainActivity.this, "掃一掃", 100).show();
break;
case 3:
Toast.makeText(MainActivity.this, "搖一搖", 100).show();
break;
}
return false;
}
});
popMenu.show();//顯示彈出的菜單
}
});
return super.onCreateOptionsMenu(menu);
}
//選擇菜單的事件監聽
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.actionbar_call:
Toast.makeText(this, "打電話", 100).show();
break;
case R.id.actionbar_camera:
Toast.makeText(this, "照相", 100).show();
break;
case R.id.actionbar_share:
Toast.makeText(this, "分享", 100).show();
break;
case android.R.id.home://返回的圖標
Intent intent = new Intent(this,MainActivity.class);
//這裏用addFlags方法
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Toast.makeText(this, "返回成功!", 100).show();
break;
}
return super.onOptionsItemSelected(item);
}
}