Android ActionBar控件操作使用詳解
ActionBar是一個(gè)顯示在屏幕頂部的控件,它包括了在左邊顯示的應(yīng)用的logo圖標(biāo)和右邊操作菜單的可見(jiàn)項(xiàng)。
實(shí)現(xiàn)方法
在ActionBar上的圖標(biāo)叫做ActionButtons,可以把不重要的ActionButtons放在ActionOverflows。
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_share"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="ifRoom"
android:title="@string/share"/>
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:showAsAction="ifRoom"
android:title="@string/search"/>
<item
android:id="@+id/action_setting"
android:showAsAction="always"
android:title="@string/setting"/>
</menu>
在Activity中:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem shareItem = menu.findItem(R.id.action_share);
return true;
}
自定義ActionBar background:
- 在Theme.xml中新建自定義Style,使其繼承已有的Action Bar Style(Theme.holo)
- 復(fù)寫(xiě)其actionBarStyle屬性
- actionBarStyle屬性值指向另一個(gè)被覆寫(xiě)了background屬性的Style
- 指定該background的屬性值
theme.xml:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomActionBarTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/CustomBackground</item>
</style>
<style name="CustomBackground" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/actionbar_background</item>
</style>
設(shè)置ActionBar為T(mén)ab樣式:
ActionBar actionBar = getActionBar(); //for <3.0 getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 0; i < 3; i++){
Tab tab = actionBar.newTab();
tab.setText("Tab" + i);
tab.setTabListener(null);
actionBar.addTab(tab);
}
實(shí)現(xiàn)Tablistener回調(diào)方法:
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Toast.makeText(MainActivity.this, "TabSelected" + tab.getPosition(), Toast.LENGTH_SHORT).show();
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
};
啟用Overlay模式
ActionBar占用一定屏幕空間,可以使之自動(dòng)隱藏,但是每次自動(dòng)隱藏又會(huì)導(dǎo)致重新計(jì)算屏幕,可以設(shè)置其為Overlay模式以把ActionBar放在屏幕的上面而不是頂部。
首先需要?jiǎng)?chuàng)建自定義theme,并設(shè)置android.windowActionBarOverlay屬性為true。
<style
name="CustomActionBarOverlayTheme"
parent="@android:style/Theme.Holo"
>
<item name="android:windowActionBarOverlay">true</item>
</style>
如果要預(yù)留一定空間,可以指定PaddingTop:
android:paddingTop="?android:attr/actionBarSize"
添加ActionProvider:
menu.xml:
<item
android:id="@+id/action_share"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="ifRoom"
android:title="@string/share"/>
在Activity代碼中:(當(dāng)前系統(tǒng)中能夠發(fā)送圖片的所有應(yīng)用)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem shareItem = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) shareItem.getActionProvider();
mShareActionProvider.setShareIntent(getDefaultIntent());
return true;
}
private Intent getDefaultIntent() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
return intent;
到此這篇關(guān)于Android ActionBar控件操作使用詳解的文章就介紹到這了,更多相關(guān)Android ActionBar控件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android實(shí)現(xiàn)按鈕點(diǎn)擊效果
本文主要介紹了Android實(shí)現(xiàn)按鈕點(diǎn)擊效果:第一次點(diǎn)擊變色,第二次恢復(fù)。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
android橫豎屏切換時(shí)候Activity的生命周期
曾經(jīng)遇到過(guò)一個(gè)面試題,讓你寫(xiě)出橫屏切換豎屏Activity的生命周期。現(xiàn)在給大家分析一下他切換時(shí)具體的生命周期是怎么樣的2013-01-01
android 仿微信demo——注冊(cè)功能實(shí)現(xiàn)(服務(wù)端)
本篇文章主要介紹了微信小程序-閱讀小程序?qū)嵗?,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望能給你們提供幫助2021-06-06
Android實(shí)現(xiàn)網(wǎng)易嚴(yán)選標(biāo)簽欄滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)易嚴(yán)選標(biāo)簽欄滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android ListView萬(wàn)能適配器實(shí)例代碼
本文主要介紹Android ListView萬(wàn)能適配器,這里整理了詳細(xì)的資料及實(shí)現(xiàn)代碼,以及實(shí)現(xiàn)效果圖,有需要的小伙伴可以參考下2016-09-09
Android實(shí)現(xiàn)長(zhǎng)截屏功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)長(zhǎng)截屏功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
Android 中Notification彈出通知實(shí)現(xiàn)代碼
NotificationManager 是狀態(tài)欄通知的管理類,負(fù)責(zé)發(fā)通知、清除通知等操作。接下來(lái)通過(guò)本文給大家介紹Android 中Notification彈出通知實(shí)現(xiàn)代碼,需要的的朋友參考下吧2017-08-08

