2. 继承ActionBarActivity
3. menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xxx="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
xxxitcast:showAsAction="always"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
xxx:showAsAction="always"/>
</menu>`
4. 在Activity中
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
搜索
1. menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xxx="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
xxx:actionViewClass="android.support.v7.widget.SearchView"
xxx:showAsAction="always"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
xxx:showAsAction="never">
</item>
</menu>
2. 自己处理ActionBar菜单的点击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_search) {
Toast.makeText(getApplicationContext(), "搜索点击了", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
处理ActionBar的返回
1. 清单文件配置
<activity
android:name="zz.xxx.googlepalyz11.DetailActivity"
android:label="@string/title_activity_detail"
android:parentActivityName="zz.xxx.googlepalyz11.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="zz.xxx.googlepalyz11.MainActivity" />
</activity>
意思是点击DetailActivity可以返回MainActivity不用再代码添加返回的监听做处理
2. 让 ActionBar的返回可用
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
给ActionBar添加tab
1.添加一个状态选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- STATES WHEN BUTTON IS NOT PRESSED -->
<!-- Non focused states -->
<item android:drawable="@drawable/tab_unselected" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/tab_selected" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
<!-- Focused states (such as when focused with a d-pad or mouse hover) -->
<item android:drawable="@drawable/tab_unselected_focused" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/tab_selected_focused" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
<!-- STATES WHEN BUTTON IS PRESSED -->
<!-- Non focused states -->
<item android:drawable="@drawable/tab_unselected_pressed" android:state_focused="false" android:state_pressed="true" android:state_selected="false"/>
<item android:drawable="@drawable/tab_selected_pressed" android:state_focused="false" android:state_pressed="true" android:state_selected="true"/>
<!-- Focused states (such as when focused with a d-pad or mouse hover) -->
<item android:drawable="@drawable/tab_unselected_pressed" android:state_focused="true" android:state_pressed="true" android:state_selected="false"/>
<item android:drawable="@drawable/tab_selected_pressed" android:state_focused="true" android:state_pressed="true" android:state_selected="true"/>
</selector>
2. 自定义样式
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
<!-- Support library compatibility -->
<item name="actionBarTabStyle">@style/MyActionBarTabs</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<!-- ActionBar tabs styles -->
<style name="MyActionBarTabs"
parent="@style/Widget.AppCompat.ActionBar.TabView">
<!-- tab indicator -->
<item name="android:background">@drawable/actionbar_tab_indicator</item>
<!-- Support library compatibility -->
<item name="background">@drawable/actionbar_tab_indicator</item>
</style>
</resources>
3. 版本兼容 v11 文件夹下添加了一个更高版本的样式
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!-- API 11 theme customizations can go here. -->
<item name="android:actionBarTabStyle">@style/MyActionBarTabs</item>
</style>
</resources>
activity页面
/** 当 搜索框内容改变的时候调用 */
@Override
public boolean onQueryTextChange(String text) {
// Toast.makeText(getApplicationContext(), "change = "+text,
// Toast.LENGTH_SHORT).show();
UIUtils.showToast(getApplicationContext(), "change = " + text);
return true;
}
/** 当 按下回车的时候调用 */
@Override
public boolean onQueryTextSubmit(String text) {
// Toast.makeText(getApplicationContext(), "Submit = "+text,
// Toast.LENGTH_SHORT).show();
UIUtils.showToast(getApplicationContext(), "Submit = " + text);
return true;
}
可以得到点击搜索的什么;
4. 显示tab&添加tab
// setup action bar for tabs
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
MyTabListener tabListener = new MyTabListener();
Tab tab = actionBar.newTab().setText("标签一").setTabListener(tabListener);
actionBar.addTab(tab);
tab = actionBar.newTab().setText("标签二").setTabListener(tabListener);
actionBar.addTab(tab);
tab = actionBar.newTab().setText("标签三").setTabListener(tabListener);
actionBar.addTab(tab);
tab = actionBar.newTab().setText("标签四").setTabListener(tabListener);
actionBar.addTab(tab);