Action Bar示例代码 (一)修改版

 今天一起来看下Android Action Bar的示例代码,我们通过活动栏做一个简单选项菜单。下面这个例子将演示ActionBar.NAVIGATION_MODE_STANDARD、ActionBar.NAVIGATION_MODE_TABS和                     : ActionBar.NAVIGATION_MODE_STANDARD等模式的效果。最后Android123仍然提示大家Action Bar是Android 3.0 honeycomb才开始有的特性,老版本的SDK和固件无法使用。

public class ActionBarDisplayOptions extends Activity implements
		View.OnClickListener, ActionBar.TabListener {
	private View mCustomView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.action_bar_display_options);

		findViewById(R.id.toggle_home_as_up).setOnClickListener(this);
		findViewById(R.id.toggle_show_home).setOnClickListener(this);
		findViewById(R.id.toggle_use_logo).setOnClickListener(this);
		findViewById(R.id.toggle_show_title).setOnClickListener(this);
		findViewById(R.id.toggle_show_custom).setOnClickListener(this);
		findViewById(R.id.toggle_navigation).setOnClickListener(this);
		findViewById(R.id.cycle_custom_gravity).setOnClickListener(this);

		mCustomView = getLayoutInflater().inflate(
				R.layout.action_bar_display_options_custom, null);
		
		final ActionBar bar = getActionBar();
		
		bar.setCustomView(mCustomView, new ActionBar.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

		bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this));
		bar.addTab(bar.newTab().setText("Tab 2").setTabListener(this));
		bar.addTab(bar.newTab().setText("Tab 3").setTabListener(this));

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.display_options_actions, menu);
		return true;

	}

	//  ActionBar.TabListener interface
	// (start)//
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	// ActionBar.TabListener interface (end)//

	// View.OnClickListener interface (start)//
	public void onClick(View v) {
		final ActionBar bar = getActionBar();
		int flags = 0;
		switch (v.getId()) {
		case R.id.toggle_home_as_up:
			flags = ActionBar.DISPLAY_HOME_AS_UP;
			break;
		case R.id.toggle_show_home:
			flags = ActionBar.DISPLAY_SHOW_HOME;
			break;
		case R.id.toggle_use_logo:
			flags = ActionBar.DISPLAY_USE_LOGO;
			break;
		case R.id.toggle_show_title:
			flags = ActionBar.DISPLAY_SHOW_TITLE;
			break;
		case R.id.toggle_show_custom:
			flags = ActionBar.DISPLAY_SHOW_CUSTOM;
			break;

		case R.id.toggle_navigation:
			bar.setNavigationMode(bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_STANDARD ? ActionBar.NAVIGATION_MODE_TABS
					: ActionBar.NAVIGATION_MODE_STANDARD);
			return;
		case R.id.cycle_custom_gravity:
			ActionBar.LayoutParams lp = (ActionBar.LayoutParams) mCustomView
					.getLayoutParams();
			int newGravity = 0;
			switch (lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
			case Gravity.LEFT:
				newGravity = Gravity.CENTER_HORIZONTAL;
				break;
			case Gravity.CENTER_HORIZONTAL:
				newGravity = Gravity.RIGHT;
				break;
			case Gravity.RIGHT:
				newGravity = Gravity.LEFT;
				break;
			}
			lp.gravity = lp.gravity & ~Gravity.HORIZONTAL_GRAVITY_MASK
					| newGravity;
			bar.setCustomView(mCustomView, lp);
			return;
		}

		int change = bar.getDisplayOptions() ^ flags;
		bar.setDisplayOptions(change, flags);

	}
	// View.OnClickListener interface (end)//

}


 接下来是文中涉及的3个xml布局文件:

 相关的XML布局action_bar_display_options.xml代码为

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    <Button android:id="@+id/toggle_home_as_up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/toggle_home_as_up" />
    <Button android:id="@+id/toggle_show_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/toggle_show_home" />
    <Button android:id="@+id/toggle_use_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/toggle_use_logo" />
    <Button android:id="@+id/toggle_show_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/toggle_show_title" />
    <Button android:id="@+id/toggle_show_custom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/toggle_show_custom" />
    <Button android:id="@+id/toggle_navigation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/toggle_navigation" />
    <Button android:id="@+id/cycle_custom_gravity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cycle_custom_gravity" />
</LinearLayout>

相关的自定义布局文件action_bar_display_options_custom.xml代码为

<?xml version="1.0" encoding="utf-8"?>

<Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:text="@string/display_options_custom_button" />


相关的menu布局xml文件display_options_actions.xml代码为

 

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/simple_item"
          android:title="@string/display_options_menu_item" />
</menu>

  上面的string后的元素大家可以根据自己的情况进行替换。

          

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值