标签:
1. 引用v7-appcompat 库3. android:theme="@style/Theme.AppCompat.Light" >
4.res/menu中的布局改为
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="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" yourapp:showAsAction="ifRoom" yourapp:actionViewClass="android.support.v7.widget.SearchView"/> </menu>
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >是为了向下兼容(3.0之前版本)
5.搜索功能
Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); SearchView searchView = (SearchView) menu.findItem(R.id.action_search) .getActionView(); searchView.setOnQueryTextListener(this);// 搜索的监听 return true; } // 当搜索提交的时候 @Override public boolean onQueryTextSubmit(String query) { Toast.makeText(getApplicationContext(), query, 0).show(); return true; } // 当搜索的文本发生变化 @Override public boolean onQueryTextChange(String newText) { //Toast.makeText(getApplicationContext(), newText, 0).show(); return true; }
/** 处理actionBar菜单条目的点击事件 */ <span style="white-space:pre"> </span>public boolean onOptionsItemSelected(MenuItem item) { <span style="white-space:pre"> </span>if (item.getItemId() == R.id.action_search) { <span style="white-space:pre"> </span>Toast.makeText(getApplicationContext(), "搜索", 0).show(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>return drawerToggle.onOptionsItemSelected(item)|super.onOptionsItemSelected(item); <span style="white-space:pre"> </span>}6.返回按钮处理,在oncreate中
ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true);//返回按钮可以被点击 actionBar.setHomeButtonEnabled(true);
<activity android:name=".DetailActivity" android:label="@string/app_detail" android:parentActivityName="com.xxx.MainActivity" > <!-- Parent activity meta-data to support 4.0 and lower --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.xxx.MainActivity" /> </activity>lable优先使用自己配置的title。
7.实现ActionBar Tab标签(类似indicator)
①在Drawable 目录下 写了一个标签的状态选择器
<?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:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected" /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected" /> <!-- Focused states (such as when focused with a d-pad or mouse hover) --> <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused" /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_focused" /> <!-- STATES WHEN BUTTON IS PRESSED --> <!-- Non focused states --> <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed" /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed" /> <!-- Focused states (such as when focused with a d-pad or mouse hover) --> <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed" /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed" /> </selector>②实现自定义主题 them.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light"> <item name="android:actionBarTabStyle">@style/MyActionBarTabs</item> <!-- Support library compatibility --> <item name="actionBarTabStyle">@style/MyActionBarTabs</item> </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>
android:theme="@style/CustomActionBarTheme"③在代码中添加
ActionBar actionBar = getSupportActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); Tab tab1=actionBar.newTab().setText("标签一").setTabListener(new MyTabListener()); actionBar.addTab(tab1);
标签:
原文地址:http://blog.csdn.net/gaobaoshen1/article/details/51350690