搜索
首页后端开发Python教程Android应用开发中Action bar编写的入门教程

从Android 3.0开始除了我们重点讲解的Fragment外,Action Bar也是一个重要的内容,Action Bar主要是用于代替传统的标题栏,对于Android平板设备来说屏幕更大它的标题使用Action Bar来设计可以展示更多丰富的内容,方便操控。

Action Bar主要功能包含:

1. 显示选项菜单
2. 提供标签页的切换方式的导航功能,可以切换多个fragment.
3. 提供下拉的导航条目.
4. 提供交互式活动视图代替选项条目
5. 使用程序的图标作为返回Home主屏或向上的导航操作。

提示在你的程序中应用ActionBar需要注意几点,SDK和最终运行的固件必须是Android 3.0即honeycomb,在androidmanifest.xml文件中的uses-sdk元素中加入android:minSdkVersion 或android:targetSdkVersion,类似

< manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="eoe.android.cwj" 
android:versionCode="1" 
android:versionName="1.0"> 
< uses-sdk android:minSdkVersion="honeycomb" /> 
< application ... > 
 
< /application> 
< /manifest> 


  如果需要隐藏Action Bar可以在你的Activity的属性中设置主题风格为NoTitleBar在你的manifest文件中,下面的代码在3.0以前是隐藏标题,而在3.0以后就是隐藏ActionBar了,代码为:

< activity android:theme="@android:style/Theme.NoTitleBar"> 

一、添加活动条目 Action Items

  对于活动条目大家可以在下图看到Android 3.0的标题右部分可以变成工具栏,下面的Save和Delete就是两个Action Items活动条目。

  下面是一个menu的layout布局文件代码

< &#63;xml version="1.0" encoding="utf-8"&#63;> 
< menu xmlns:android="http://schemas.android.com/apk/res/android"> 
< item android:id="@+id/menu_add" 
android:icon="@drawable/ic_menu_save" 
android:title="@string/menu_save" 
android:showAsAction="ifRoom|withText" /> 
< /menu> 

  而其他代码类似Activity中的Menu,比如

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
switch (item.getItemId()) { 
case android.R.id.home: 
// 当Action Bar的图标被单击时执行下面的Intent 
Intent intent = new Intent(this, Android123.class); 
startActivity(intent); 
break; 
} 
return super.onOptionsItemSelected(item); 
} 

对于ActionBar的创建,可以在你的Activity中重写onStart方法:

@Override 
protected void onStart() { 
super.onStart(); 
ActionBar actionBar = this.getActionBar(); 
actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP); 
} 

 调用getActionBar方式在你的Activity的onCreate中时需要注意必须在调用了setContentView之后。

二、添加活动视图 Action View

对于ActionView,我们可以在menu的布局文件使用中来自定义searchview布局,如下:

< item android:id="@+id/menu_search" 
android:title="Search" 
android:icon="@drawable/ic_menu_search" 
android:showAsAction="ifRoom" 
android:actionLayout="@layout/searchview" /> 

 也可以直接指定Android系统中的SearchView控件,那么这时menu"_search的代码要这样写:

< item android:id="@+id/menu_search" 
android:title="Search" 
android:icon="@drawable/ic_menu_search" 
android:showAsAction="ifRoom" 
android:actionViewClass="android.widget.SearchView" /> 

  大家注意上面的两种方法中一个属性是actionLayout制定一个layout xml布局文件,一个是actionViewClass指定一个类,最终调用可以在Activity中响应onCreateOptionsMenu方法映射这个menu布局即可.

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
getMenuInflater().inflate(R.menu.options, menu); 
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); 
return super.onCreateOptionsMenu(menu); 
} 

三、添加标签 Tabs

  在ActionBar中实现标签页可以实现android.app.ActionBar.TabListener ,重写onTabSelected、onTabUnselected和onTabReselected方法来关联Fragment。代码如下:

private class MyTabListener implements ActionBar.TabListener { 
 private TabContentFragment mFragment; 
 public TabListener(TabContentFragment fragment) { 
 mFragment = fragment; 
 } @Override 
 public void onTabSelected(Tab tab, FragmentTransaction ft) { 
 ft.add(R.id.fragment_content, mFragment, null); 
 } 
 @Override 
 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
 ft.remove(mFragment); 
 } 
 @Override 
 public void onTabReselected(Tab tab, FragmentTransaction ft) { 
 } 
 
} 

接下来我们创建ActionBar在Activity中,代码如下;

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
final ActionBar actionBar = getActionBar(); //提示getActionBar方法一定在setContentView后面 
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); 
Fragment artistsFragment = new ArtistsFragment(); 
actionBar.addTab(actionBar.newTab().setText(R.string.tab_artists).setTabListener(new TabListener(artistsFragment))); 
Fragment albumsFragment = new AlbumsFragment(); 
actionBar.addTab(actionBar.newTab().setText(R.string.tab_albums).setTabListener(new TabListener(albumsFragment))); 
}

四、添加下拉导航 Drop-down Navigation:

创建一个SpinnerAdapter提供下拉选项,和Tab方式不同的是Drop-down只需要修改下setNavigationMode的模式,将ActionBar.NAVIGATION_MODE_TABS改为ActionBar.NAVIGATION_MODE_LIST,最终改进后的代码为

ActionBar actionBar = getActionBar(); 
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); 
actionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback); 

上面我们通过setListNavigationCallbacks方法绑定一个SpinnerAdapter控件,具体的OnNavigationListener代码示例为;

mOnNavigationListener = new OnNavigationListener() { 
 String[] strings = getResources().getStringArray(R.array.action_list); 
 @Override 
 public boolean onNavigationItemSelected(int position, long itemId) { 
 ListContentFragment newFragment = new ListContentFragment(); 
 FragmentTransaction ft = openFragmentTransaction(); 
 ft.replace(R.id.fragment_container, newFragment, strings[position]); 
 ft.commit(); 
 return true; 
} 
 
}; 


而其中的ListContentFragment的代码为:

public class ListContentFragment extends Fragment { 
private String mText; 
 
@Override 
public void onAttach(Activity activity) { 
super.onAttach(activity); 
mText = getTag(); 
} 
 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) { 
TextView text = new TextView(getActivity()); 
text.setText(mText); 
return text; 
} 
} 

五、实现切换Tabs标签;
  
Activity代码:  

public class ActionBarTabs extends Activity { 
 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.action_bar_tabs); 
} 
 
public void onAddTab(View v) { 
final ActionBar bar = getActionBar(); 
final int tabCount = bar.getTabCount(); 
final String text = "Tab " + tabCount; 
 
bar.addTab(bar.newTab().setText(text) 
.setTabListener(new TabListener(new TabContentFragment(text)))); 
} 
 
public void onRemoveTab(View v) { 
final ActionBar bar = getActionBar(); 
bar.removeTabAt(bar.getTabCount() - 1); 
} 
 
public void onToggleTabs(View v) { 
final ActionBar bar = getActionBar(); 
 
if (bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) { 
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
 
bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE); 
} else { 
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); 
} 
} 
 
public void onRemoveAllTabs(View v) { 
getActionBar().removeAllTabs(); 
} 
 
private class TabListener implements ActionBar.TabListener { 
private TabContentFragment mFragment; 
public TabListener(TabContentFragment fragment) { 
 
mFragment = fragment; 
} 
 
public void onTabSelected(Tab tab, FragmentTransaction ft) { 
ft.add(R.id.fragment_content, mFragment, mFragment.getText()); 
} 
 
 
public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
ft.remove(mFragment); 
} 
 
public void onTabReselected(Tab tab, FragmentTransaction ft) { 
Toast.makeText(ActionBarTabs.this, "Reselected!", Toast.LENGTH_SHORT).show(); 
} 
 
} 
 
private class TabContentFragment extends Fragment { 
private String mText; 
public TabContentFragment(String text) { 
mText = text; 
} 
 
public String getText() { 
return mText; 
} 
   
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) { 
View fragView = inflater.inflate(R.layout.action_bar_tab_content, container, false); 
TextView text = (TextView) fragView.findViewById(R.id.text); 
text.setText(mText); 
return fragView; 
} 
} 
} 

涉及的布局文件action_bar_tabs.xml代码为:

< &#63;xml version="1.0" encoding="utf-8"&#63;> 
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 
 
< FrameLayout android:id="@+id/fragment_content" 
android:layout_width="match_parent" 
android:layout_height="0dip" 
android:layout_weight="1" /> 
 
< LinearLayout android:layout_width="match_parent" 
android:layout_height="0dip" 
android:layout_weight="1" 
android:orientation="vertical"> 
 
< Button android:id="@+id/btn_add_tab" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/btn_add_tab" 
android:onClick="onAddTab" /> 
 
< Button android:id="@+id/btn_remove_tab" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/btn_remove_tab" 
android:onClick="onRemoveTab" /> 
 
< Button android:id="@+id/btn_toggle_tabs" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/btn_toggle_tabs" 
android:onClick="onToggleTabs" /> 
 
< Button android:id="@+id/btn_remove_all_tabs" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/btn_remove_all_tabs" 
android:onClick="onRemoveAllTabs" /> 
< /LinearLayout> 
 
< /LinearLayout> 

布局文件action_bar_tab_content.xml;

< &#63;xml version="1.0" encoding="utf-8"&#63;> 
< TextView xmlns:android="http://schemas.android.com/apk/res/android" 
 
android:id="@+id/text" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" /> 

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python和时间:充分利用您的学习时间Python和时间:充分利用您的学习时间Apr 14, 2025 am 12:02 AM

要在有限的时间内最大化学习Python的效率,可以使用Python的datetime、time和schedule模块。1.datetime模块用于记录和规划学习时间。2.time模块帮助设置学习和休息时间。3.schedule模块自动化安排每周学习任务。

Python:游戏,Guis等Python:游戏,Guis等Apr 13, 2025 am 12:14 AM

Python在游戏和GUI开发中表现出色。1)游戏开发使用Pygame,提供绘图、音频等功能,适合创建2D游戏。2)GUI开发可选择Tkinter或PyQt,Tkinter简单易用,PyQt功能丰富,适合专业开发。

Python vs.C:申请和用例Python vs.C:申请和用例Apr 12, 2025 am 12:01 AM

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。 Python以简洁和强大的生态系统着称,C 则以高性能和底层控制能力闻名。

2小时的Python计划:一种现实的方法2小时的Python计划:一种现实的方法Apr 11, 2025 am 12:04 AM

2小时内可以学会Python的基本编程概念和技能。1.学习变量和数据类型,2.掌握控制流(条件语句和循环),3.理解函数的定义和使用,4.通过简单示例和代码片段快速上手Python编程。

Python:探索其主要应用程序Python:探索其主要应用程序Apr 10, 2025 am 09:41 AM

Python在web开发、数据科学、机器学习、自动化和脚本编写等领域有广泛应用。1)在web开发中,Django和Flask框架简化了开发过程。2)数据科学和机器学习领域,NumPy、Pandas、Scikit-learn和TensorFlow库提供了强大支持。3)自动化和脚本编写方面,Python适用于自动化测试和系统管理等任务。

您可以在2小时内学到多少python?您可以在2小时内学到多少python?Apr 09, 2025 pm 04:33 PM

两小时内可以学到Python的基础知识。1.学习变量和数据类型,2.掌握控制结构如if语句和循环,3.了解函数的定义和使用。这些将帮助你开始编写简单的Python程序。

如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础?如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础?Apr 02, 2025 am 07:18 AM

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到?如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到?Apr 02, 2025 am 07:15 AM

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器