ActionBar点击弹出下拉框操作

首先:

 

getActionBar().setDisplayShowTitleEnabled(false);

		ActionBar.LayoutParams lp = new ActionBar.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
		lp.gravity = lp.gravity & ~Gravity.HORIZONTAL_GRAVITY_MASK | Gravity.LEFT;
		ActionBarPopupButton popupButton = new ActionBarPopupButton(this);
		popupButton.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, cls[item.getOrder()]);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				if (item.getOrder() == 0) {
					intent.putExtra(Extras.DATE, mDates.get(mViewPager.getCurrentItem()));
				}
				startActivity(intent);
				return false;
			}
		});
		getActionBar().setCustomView(popupButton, lp);
		int change = getActionBar().getDisplayOptions() ^ ActionBar.DISPLAY_SHOW_CUSTOM;
		getActionBar().setDisplayOptions(change, ActionBar.DISPLAY_SHOW_CUSTOM);


private Class<?>[] cls = new Class[] { HistoryActivity.class, VibrateActivity.class, CircleActivity.class, PersonalActivity.class, DeviceActivity.class };

 


第二:

public class ActionBarPopupButton extends Button implements OnClickListener {

	private PopupMenu mPopupMenu;

	public ActionBarPopupButton(Context context) {
		super(context);
		setBackgroundResource(R.drawable.abc_spinner_ab_holo_dark);
		setTextAppearance(getContext(), android.R.style.Widget_Spinner);
		setText(getContext().getString(R.string.title_activity_detail));
		setTextColor(getResources().getColor(R.color.white));
		setOnClickListener(this);
		mPopupMenu = new PopupMenu(getContext(), this);
		mPopupMenu.inflate(R.menu.main_navigation_list);
	}

	@Override
	public void onClick(View v) {
		mPopupMenu.show();
	}

	public void setOnMenuItemClickListener(OnMenuItemClickListener listener) {
		mPopupMenu.setOnMenuItemClickListener(listener);
	}
}

setBackgroundResource 图片

setTextAppearance  样式

setText  文本内容

setTextColor   颜色

main_navigation_list  XML

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:orderInCategory="0"
        android:showAsAction="never"
        android:title="@string/title_activity_history"/>
    <item
        android:orderInCategory="1"
        android:showAsAction="never"
        android:title="@string/title_activity_vibrate"/>
    <item
        android:orderInCategory="2"
        android:showAsAction="never"
        android:title="@string/title_activity_circle_2"/>
    <item
        android:orderInCategory="3"
        android:showAsAction="never"
        android:title="@string/title_activity_personal"/>
    <item
        android:orderInCategory="4"
        android:showAsAction="never"
        android:title="@string/title_activity_device"/>

</menu>

 


 

posted @ 2016-01-12 10:52  blfshiye  阅读(338)  评论(0编辑  收藏  举报