提示:Android官网的Trainning中有Action Bar的使用教程
1. Action Bar在Android3.0(API 11)被引入,要兼容2.1以上版本,需要引入v7库(含有版本不同的v4包,需要把版本较低的包删掉)
2. 将继承的Activity改为ActionBarActivity(ActionBarActivity继承了FragmentActivity)
3. 主题改为 "Theme.AppCompat.Light"(引入v7包才有这个主题)
1. 搜索按钮配置
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:yourapp="http://schemas.android.com/apk/res-auto" ><span style="color:#3333FF;"><!-- 兼容2.x要加上这个命名空间 --></span> <!-- 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" /><span style="color:#3333FF;"><!-- 兼容2.x要用v7里的SearchView --></span> </menu>
2. 实现搜索功能
在API Demos / App / Action Bar / Action Bar Usage 中可看到搜索功能的实现效果,导入API Demos项目,参考它的代码即可。
鼠标右击-->New-->Other-->Android Sample Project-->随便勾选一个Android x.x-->next-->ApiDemos-->Finish
照抄代码
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); //安卓3.0(API 11)以上 // SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); //SearchView兼容低版本方法 SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search)); searchView.setOnQueryTextListener(this); return true; } <pre name="code" class="java"> //处理ActionBar菜单条目点击事件 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_search: Toast.makeText(getApplicationContext(), "搜索", 0).show(); return true; } return super.onOptionsItemSelected(item); } //当搜索提交时 @Override public boolean onQueryTextSubmit(String query) { Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show(); return true;//true表示要执行这个方法 } //当文本发生变化时 @Override public boolean onQueryTextChange(String newText) { Toast.makeText(this, "Searching for: " + newText + "...", Toast.LENGTH_SHORT).show(); return true;//true表示要执行这个方法 }
当我们从主页跳转到其他页面时,点击按钮(箭头)可以返回主页
1. 添加返回按钮
//ActionBar actionBar = getActionBar();//android3.0以上 ActionBar actionBar = getSupportActionBar();//兼容2.x actionBar.setDisplayHomeAsUpEnabled(true);
方法一
@Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home){ //返回按钮的id是android.R.id.home finish(); } return super.onOptionsItemSelected(item); }方法二(AndroidManifest.xml里配置)
要点:(1)<activity>中设置属性parentActivityName,指定要返回的页面;(2)配置<meta-data>来兼容4.0以下版本
<!-- The main/home activity (it has no parent activity) --> <activity android:name="com.example.myfirstapp.MainActivity" ...> ... </activity> <!-- A child of the main activity --> <activity android:name="com.example.myfirstapp.DisplayMessageActivity" android:label="@string/title_activity_display_message" android:parentActivityName="com.example.myfirstapp.MainActivity" > <!-- Parent activity meta-data to support 4.0 and lower --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.myfirstapp.MainActivity" /> </activity>
1. 配置actionBar标签指针
res/drawable/actionbar_tab_indicator.xml
<?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>
2.. 重新配置主题
<?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> 3.0以上 --> <!-- 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> <!-- 使用步骤1配置的标签指针 --> <!-- Support library compatibility --> <item name="background">@drawable/actionbar_tab_indicator</item> </style> </resources>
3. 使用这个主题
<application android:theme="@style/CustomActionBarTheme" >
4. 自定义PagerAdapter继承FragmentStatePagerAdapter (比PagerAdapter方便)
private class MainAdapter extends FragmentStatePagerAdapter{ public MainAdapter(FragmentManager fm) { super(fm); } //每个条目返回的fragment @Override public Fragment getItem(int arg0) { return new MyFragment(); } @Override public int getCount() { return mActionBar.getTabCount(); } }
5. 让标签栏与ViewPager建立关系
ViewPager mViewPager = (ViewPager) findViewById(R.id.vp); <pre name="code" class="java">ActionBar mActionBar = getSupportActionBar();//3.0以上可用getActionBar() mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);//添加标签栏 Tab tab1 = mActionBar.newTab().setText("TAB1").setTabListener(listener); Tab tab2 = mActionBar.newTab().setText("TAB2").setTabListener(listener); Tab tab3 = mActionBar.newTab().setText("TAB3").setTabListener(listener); mActionBar.addTab(tab1);//添加标签 mActionBar.addTab(tab2); mActionBar.addTab(tab3); mViewPager.setAdapter(new MainAdapter(getSupportFragmentManager())); mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){ //SimpleOnPageChangeListener比OnPageChangeListener简单方便 @Override public void onPageSelected(int position) { mActionBar.setSelectedNavigationItem(position); } });
五、添加可滑动的标签栏(PagerTabStrip)
1. 添加PagerTabStrip布局
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.PagerTabStrip android:id="@+id/pager_title_strip" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:background="#33b5e5" android:textColor="#fff" android:paddingTop="4dp" android:paddingBottom="4dp" /> </android.support.v4.view.ViewPager>
2. adapter里设置标签title
private class MainAdapter extends FragmentStatePagerAdapter{ @Override public int getCount() { return 3; // 标签个数 } // 每个标签的标题 @Override public CharSequence getPageTitle(int position) { return "标签" + (position + 1); } }