标签:
ActionBar
类来控制action
bar 。android:windowActionBarOverlay=true,更多信息请查看。
添加action items(活动项)
有时候你需要在选项菜单中给用户一些直接的选项,为了实现它,必须在action bar中声明这些菜单项(menu item)并作为活动项。
一个活动项包含一个icon和text title,如果一个菜单项没有作为活动项显示,那么系统会将它放在溢出菜单(overflow menu)中。
当activitty第一次启动,系统通过调用
onCreateOptionsMenu()填充action
bar 和溢出菜单。@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
return true;
}
android:title
、 android:icon
但安卓只会默认的显示icon。如果需要显示文字,必须在
android:showAsAction中加上withText。可能由于空间问题文字显示不全。<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_save"
android:icon="@drawable/ic_menu_save"
android:title="@string/menu_save"
android:showAsAction="ifRoom|withText" />
</menu>
onOptionsItemSelected()
方法,并且传递android.R.id.home
ID。作为响应,应该启动主actiivty或者返回上一个activity。FLAG_ACTIVITY_CLEAR_TOP有了这个flag,它会销毁在主activity之前的所有activity,以确保主activity在第一个(栈顶)。
下面是一个点击icon返回主activity的片段
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
FLAG_ACTIVITY_NEW_TASK
flag。事实上,当你的app需要接受来自其他app的intent,都需要加入该flag。setDisplayHomeAsUpEnabled(true)
函数,其它设置同上。android:actionLayout
或者android:actionViewClass
属性<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_search"
android:title="@string/menu_search"
android:icon="@drawable/ic_menu_search"
android:showAsAction="ifRoom|collapseActionView"//表明需要嵌入一个button中,当用户点击该按钮,view展开。
android:actionViewClass="android.widget.SearchView" />
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Configure the search info and add any event listeners
...
return super.onCreateOptionsMenu(menu);
}
SearchView
.
这个类。当然,一些设备也会提供专有的查找按钮,使用时只需在onKeyUp()
监听 KEYCODE_SEARCH
事件,然后调用expandActionView()
.@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
MenuItem menuItem = menu.findItem(R.id.actionItem);
...
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true; // Return true to expand action view
}
});
}
ActionBar.TabListener
ActionBar.Tab
and
通过调用setTabListener()设置
ActionBar.TabListener
.
也还要设置tab的title/icon 通过 setText()
/ setIcon()
.addTab()
.将每一个tab加入action
bar。public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
/** Constructor used each time a new tab is created.
* @param activity The host Activity, used to instantiate the fragment
* @param tag The identifier tag for the fragment
* @param clz The fragment‘s Class, used to instantiate the fragment
*/
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
/* The following are each of the ActionBar.TabListener callbacks */
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Notice that setContentView() is not used, because we use the root
// android.R.id.content as the container for each fragment
// setup action bar for tabs
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
Tab tab = actionBar.newTab()
.setText(R.string.artist)
.setTabListener(new TabListener<ArtistFragment>(
this, "artist", ArtistFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText(R.string.album)
.setTabListener(new TabListener<AlbumFragment>(
this, "album", AlbumFragment.class));
actionBar.addTab(tab);
}
getSelectedNavigationIndex()获得当前选中的选项卡的index。
使用下拉的导航(
drop-down Navigation )SpinnerAdapter
用于提供下拉的选项和布局ActionBar.OnNavigationListener
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list,
android.R.layout.simple_spinner_dropdown_item);
<?xml version="1.0" encoding="utf-8"?>
<!---数组文件--->
<resources>
<string-array name="action_list">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
</string-array>
</pre>
mOnNavigationListener = new OnNavigationListener() {
// Get the same strings provided for the drop-down‘s ArrayAdapter
String[] strings = getResources().getStringArray(R.array.action_list);
@Override
public boolean onNavigationItemSelected(int position, long itemId) {
// Create new fragment from our own Fragment class
ListContentFragment newFragment = new ListContentFragment();
FragmentTransaction ft = openFragmentTransaction();
// Replace whatever is in the fragment container with this fragment
// and give the fragment a tag name equal to the string at the position selected
ft.replace(R.id.fragment_container, newFragment, strings[position]);
// Apply changes
ft.commit();
return true;
}
};
DeviceDefault
主题下默认的绘制是不透明的。<SomeView
...
android:layout_marginTop="?android:attr/actionBarSize" />
getHeight()
.获得action
bar的高度,如果在activity生命周期的早期调用这个方法,那么调用时返回的高度并不包括堆叠操作栏(因为导航标签)。要看如何在运行时判断操作栏总的高度(包括被堆放的操作栏),请看Honeycomb
Gallery示例应用中的TitlesFragment类。android:actionButtonStyle
android:actionBarItemBackground
android:itemBackground
android:actionBarDivider
android:actionMenuTextColor
android:actionMenuTextAppearance
android:actionBarWidgetTheme
android:actionBarTabStyle
android:actionBarTabBarStyle
android:actionBarTabTextStyle
android:actionDropDownStyle
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActivityTheme" parent="@android:style/Theme.Holo">
<item name="android:actionBarTabTextStyle">@style/CustomTabTextStyle</item>
<item name="android:actionBarDivider">@drawable/ab_divider</item>
<item name="android:actionBarItemBackground">@drawable/ab_item_background</item>
</style>
<!-- style for the action bar tab text -->
<style name="CustomTabTextStyle" parent="@android:style/TextAppearance.Holo">
<item name="android:textColor">#2456c2</item>
</style>
</resources>
<style>
标签都必须有一个父主题。这样当一些样式你没有指定的时候会使用父主题的样式。android:actionBarStyle
and android:actionBarSplitStyle
可以定义action
bar的各种属性,包括 android:background
, android:backgroundSplit
,
and android:backgroundStacked
.
的不同背景。同样,确保有个父主题。<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActivityTheme" parent="@android:style/Theme.Holo">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<!-- other activity and action bar styles here -->
</style>
<!-- style for the action bar backgrounds -->
<style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">
<item name="android:background">@drawable/ab_background</item>
<item name="android:backgroundStacked">@drawable/ab_background</item>
<item name="android:backgroundSplit">@drawable/ab_split_background</item>
</style>
</resources>
标签:
原文地址:http://blog.csdn.net/yh_android_blog/article/details/51828488