[翻译]Action Bar for Android

ActionBar for Android

这个项目旨在提供一个可重复使用的ACTION BAR的组成部分。有关Action Bar 模式的详细描述可以参考Android Patterns.

Action Bar 组件是一个 Library Project ,这意味着无需复制资源到项目,仅仅需要将Action Bar组件引用到项目中。

Action Bar 需要 icons 么?

Need icons to your action bar? Olof Brickarp has ported some of Androids native icons to vector format.

使用

在 layout 文件中

<com.markupartist.android.widget.ActionBar
    android:id="@+id/actionbar"
    app:title="@string/some_title"
    style="@style/ActionBar"
    />

app:title 是可选的,也可以在程序中使用 setTitle 给 ActionBar 设置标题。

The use of app:title is optional, it's also possible to assign the title using the setTitle programmatically on the ActionBar. To be able to use the more convenient app:title the application namespace must be included in the same manner as the android namespace is. Please refer to the layout other.xml in the example project for a full example. Again, note that it's the application namespace and not the actionbar namespace that must be referred like xmlns:app="http://schemas.android.com/apk/res/you.application.package.here".

在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.addAction(new IntentAction(this, createShareIntent(), R.drawable.ic_title_share_default));
actionBar.addAction(new ToastAction());

自定义Action

创建自定义Action 仅需要实现一个Action接口,例如:

private class ToastAction implements Action {

    @Override
    public int getDrawable() {
        return R.drawable.ic_title_export_default;
    }

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

}

Title 点击事件

处理title的点击事件,仅仅需要调用ActionBar的setOnTitleClickListener方法,设置一个android.view.View.OnClickListener。在onClick方法中的View对象就是title所在的TextView。

actionBar.setOnTitleClickListener(new OnClickListener() {
    public void onClick(View v) {
        // Your code here
    }
});

定制

由于ActionBar是作为一个Library Project,所有的资源都会通过引用的方式合并到新项目中。在新项目中设置的参数将会覆盖在ActionBar中默认指定的参数。

如果不喜欢ActionBar在colors.xml 设置的的默认参数,只需要在新的项目中的colors.xml中覆盖默认设置即可。如果要设置一个蓝色的ActionBar,只需要在colors.xml中按照如下设置即可。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="actionbar_separator">#3A5FCD</color>
    <color name="actionbar_background_start">#3A5FCD</color>
    <color name="actionbar_background_end">#27408B</color>
</resources> 

同样,也可以在新项目中覆盖drawables,layouts,以及其他的ActionBar属性。

Contributions

This widget wouldn't be the same without the excellent contributions by;

Want to contribute?

GitHub has some great articles on how to get started with Git and GitHub and how to fork a project.

Contributers are recommended to fork the app on GitHub (but don't have too). Create a feature branch, push the branch to git hub, press Pull Request and write a simple explanation.

One fix per commit. If say a a commit closes the open issue 12. Just add closes #12 in your commit message to close that issue automagically.

All code that is contributed must be compliant with Apache License 2.0.

Code Style Guidelines

Contributers are recommended to follow the Android Code Style Guidelines with exception for line length that I try to hold to 80 columns where possible.

In short that is;

  • Indentation: 4 spaces, no tabs.
  • Line length: 80 columns
  • Field names: Non-public, non-static fields start with m.
  • Braces: Opening braces don't go on their own line.
  • Acronyms are words: Treat acronyms as words in names, yielding XmlHttpRequest, getUrl(), etc.
  • Consistency: Look at what's around you!

Have fun and remember we do this in our spare time so don't be too serious :)

https://github.com/johannilsson/android-actionbar

posted @ 2011-06-03 06:47  熊波  阅读(4087)  评论(1编辑  收藏  举报