Action Bar for Android
https://www.cnblogs.com/xiongbo/archive/2011/06/03/2063468.html
這個項目旨在提供一個可重複使用的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:
app:title="@string/some_title"
/>
app:title 是可選的,也可以在程序中使用 setTitle 給 ActionBar 設置標題。
The use of app:title
is optional, it's also possible to assign the title using the setTitle
programmatically
on theActionBar
. 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 likexmlns:app="https://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, https://blog.fsck.com
- Gyuri Grell, https://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
最後更新:2017-04-02 17:09:26