[翻譯]Action Bar for Android

來源:互聯網
上載者:User
文章目錄
  • 在 layout 檔案中
  • 在Activity中,HomeAction處於Bar的最左側,普通Action處於Bar的最右側
  • 自訂Action
  • Title 點擊事件
  • 定製
  • Want to contribute?

這個項目旨在提供一個可重複使用的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;

  • ohhorob, https://github.com/ohhorob
  • denravonska, https://github.com/denravonska
  • rpdillon, https://github.com/rpdillon
  • RickardPettersson, https://github.com/RickardPettersson
  • Jake Wharton, https://github.com/JakeWharton
  • Jesse Vincent, http://blog.fsck.com
  • Gyuri Grell, http://gyurigrell.com
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.