pullList属性使用效果图示例:
//不用布局
//MainActivity 类
代码
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//===============pullList的使用============================
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下
1、创建一个菜单文件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>
============================
2、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("返回");
}
//1、创建一个选择菜单
@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);
}
}