1. 程式人生 > >Android UI開發第二十五篇——分享一篇自定義的 Action Bar

Android UI開發第二十五篇——分享一篇自定義的 Action Bar

       Action Bar是android3.0以後才引入的,主要是替代3.0以前的menu和tittle bar。在3.0之前是不能使用Action Bar功能的。這裡引入了自定義的Action Bar,自定義Action bar也不是完全實現了 Action bar功能,只是在外形上相似。自定義Action bar沒有實現overflow button(懸浮按鈕)的功能,如果想進一步實現overflow button功能,可參考Android UI開發第十六篇——分享一個popuwindow例項.

xml

 <com.nedu.android.widget.ActionBar
	    android:id="@+id/actionbar"
	    app:title="@string/some_title"
	    style="@style/ActionBar"
        />
 app:title 可選,也可以在使用時設定,actionBar.setTitle("Home")。
在Activity中,HomeAction處於Bar的最左側,普通Action處於Bar的最右側
        ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
        // You can also assign the title programmatically by passing a
        // CharSequence or resource id.
        //actionBar.setTitle(R.string.some_title);
        actionBar.setHomeAction(new IntentAction(this, HomeActivity.createIntent(this), R.drawable.ic_title_home_default));
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.addAction(new IntentAction(this, createShareIntent(), R.drawable.ic_title_share_default));
        actionBar.addAction(new ExampleAction());
自定義Action建立自定義Action 僅需要實現一個Action介面,例如ExampleAction:
  private class ExampleAction extends AbstractAction {

        public ExampleAction() {
            super(R.drawable.ic_title_export_default);
        }

        @Override
        public void performAction(View view) {
            Toast.makeText(OtherActivity.this,
                    "Example action", Toast.LENGTH_SHORT).show();
        }

    }

如果想修改UI屬性可修改drawable、layout、values裡面的檔案。

/**
* @author 張興業
* 郵箱:xy-zhang#163.com
* android開發進階群:278401545
*
*/