Android ActionBar总结一
2015-08-24 14:43
911 查看
转载请注明 /article/1462862.html
这部分是对Android官方doc里Actionbar内容的总结。
1. Android API 3.0及以上版本的
2. Android API 2.1及以上版本的
如果你要兼容低版本的,那就选择第二种方式。如果你觉得低版本已经不在考虑范围了,建议你选择第一种方式。因为第二种方式的显示和第一种有一些差异。
然后主题选择Theme.Holo
然后需要设置主题,你可以根据需求设在Application或者Activity下。 然后使用Theme.AppCompat…。
如果你要自定义主题,要确保该主题继承自Theme.AppCompat…
然后你的最低sdk可以设置为7
“超出部分”是指如下图中的3:
怎么定义呢?举个例子。
res/menu/main_activity_actions.xml
这里的showAsAction属性是用来定义action的显示情况,如果是ifRoom,意思是如果有空间,那就显示;如果空间不够了,那就显示到“超出部分”里。而never的意思就是直接放到“超出部分”里。
它是按定义的先后顺序,从左到右显示的。所以定义的时候,可以都设置为ifRoom,让系统去判断是否需要显示下。
图标大小也是有要求的,比方说,在drawable-xhdpi里放64x64大小的图标,在drawable-xxhdpi里放96x96大小的图标,这样显示出来的效果才最好。
这里,我把官方的图标上传了,供有需要的朋友下载。
Android_Design_Icons
如果你要在Android2.1版本上使用,showAsAction这个属性就无法从android那里获取到,而要用support v7里的属性。
res/menu/main_activity_actions.xml
然后响应定义在onOptionsItemSelected里:
那么怎么设置呢?
先是在manifest里定义parentActivityName属性。如果是要支持2.1以上版本,要通过meta-data里的属性。
然后在代码里调用setDisplayHomeAsUpEnabled(true)
这样一来,点击图标左边的箭头,就会退出该activity。
Theme.Holo
Theme.Holo.Light
Theme.Holo.Light.DarkActionBar
使用方式,可以定义在application下或者activity下,如下:
支持2.1以上的主题如下:
Theme.AppCompat
Theme.AppCompat.Light
Theme.AppCompat.Light.DarkActionBar
如果你使用的是navigation tabs或者是split action bar,你要使用到backgroundStacked和backgroundSplit这两个属性。
注意一点的是,自定义主题时,一定要继承自上面介绍的这些主题,除非你想要把里面的所有原先存在的属性都推翻自己再定义一次。
res/values/themes.xml
然后在manifest里使用:
res/values/themes.xml
这里可以看到,定义了两个版本。官方的建议是用support v7实现的,最好两个版本都定义,这样一来,如果你使用的手机是3.0以上的,v7库会自动使用android原生的actionbar,而不是v7库里实现的版本。
然后在activity或者application里使用
1.Action bar title:在actionBarStyle里复写titleTextStyle属性。
Note: The custom style applied to titleTextStyle should use TextAppearance.Holo.Widget.ActionBar.Title as the parent style.
2.Action bar tabs:在你的activity或application主题里,复写actionBarTabTextStyle
3.Action buttons:在你的activity或application主题里,复写actionMenuTextColor
res/values/themes.xml
res/values/themes.xml
然后这个backgroud指定的drawable可以使用如下drawable:
res/drawable/actionbar_tab_indicator.xml
res/values/themes.xml
res/values/themes.xml
如果遇到要不断显示隐藏actionbar,你可以增加一个悬浮属性,这样,actionbar就会浮在activity上,显示和隐藏就不会影响到activity重新布局了。
如果你要让actionbar半透明状态,只要设置背景半透明就行,比方说如下图的情况:
至此,actionbar的官方基础教程就介绍完了,下一篇Android ActionBar总结二继续讲述Actionbar更多功能。
这部分是对Android官方doc里Actionbar内容的总结。
建立Action Bar
建立Action Bar分为两种环境下的建立方式:1. Android API 3.0及以上版本的
2. Android API 2.1及以上版本的
如果你要兼容低版本的,那就选择第二种方式。如果你觉得低版本已经不在考虑范围了,建议你选择第一种方式。因为第二种方式的显示和第一种有一些差异。
API 3.0及以上
你只要把manifest的最低sdk设为3.0开始就行<manifest ... > <uses-sdk android:minSdkVersion="11" ... /> ... </manifest>
然后主题选择Theme.Holo
API 2.1及以上
你需要先把support v7包放到工程的libs下,然后你需要展示ActionBar的Activity要继承AppCompatActivity(官网使用的是ActionBarActivity,不过这个已经过时,不推荐使用了,可以用AppCompatActivity替代)public class MainActivity extends AppCompatActivity { ... }
然后需要设置主题,你可以根据需求设在Application或者Activity下。 然后使用Theme.AppCompat…。
<activity android:theme="@style/Theme.AppCompat.Light" ... >
如果你要自定义主题,要确保该主题继承自Theme.AppCompat…
然后你的最低sdk可以设置为7
<manifest ... > <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="18" /> ... </manifest>
添加Action Buttons
xml定义actions
所有Actionbars和其他显示在“超出部分”的items都是定义在res/menu资源文件里。“超出部分”是指如下图中的3:
怎么定义呢?举个例子。
res/menu/main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- Search, should appear as action button --> <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search" android:showAsAction="ifRoom" /> <!-- Settings, should always be in the overflow --> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:showAsAction="never" /> </menu>
这里的showAsAction属性是用来定义action的显示情况,如果是ifRoom,意思是如果有空间,那就显示;如果空间不够了,那就显示到“超出部分”里。而never的意思就是直接放到“超出部分”里。
它是按定义的先后顺序,从左到右显示的。所以定义的时候,可以都设置为ifRoom,让系统去判断是否需要显示下。
图标大小也是有要求的,比方说,在drawable-xhdpi里放64x64大小的图标,在drawable-xxhdpi里放96x96大小的图标,这样显示出来的效果才最好。
这里,我把官方的图标上传了,供有需要的朋友下载。
Android_Design_Icons
如果你要在Android2.1版本上使用,showAsAction这个属性就无法从android那里获取到,而要用support v7里的属性。
res/menu/main_activity_actions.xml
<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" /> ... </menu>
添加Action响应
在xml里定义好之后,需要把这个menu关联到Actionbar,通过onCreateOptionsMenu。@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu items for use in the action bar MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_activity_actions, menu); return super.onCreateOptionsMenu(menu); }
然后响应定义在onOptionsItemSelected里:
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle presses on the action bar items switch (item.getItemId()) { case R.id.action_search: openSearch(); return true; case R.id.action_settings: openSettings(); return true; default: return super.onOptionsItemSelected(item); } }
为多层activity添加返回按钮
一般应用包含很多activity,比方说,从A activity进入了B activity,然后我要退出B,我们常规的做法是可以添加一个返回按钮,或者不加任何东西,通过Back按键,让系统回收这个activity。而Actionbar为我们提供了更便捷的方式。它的显示效果如下:那么怎么设置呢?
先是在manifest里定义parentActivityName属性。如果是要支持2.1以上版本,要通过meta-data里的属性。
<application ... > ... <!-- 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> </application>
然后在代码里调用setDisplayHomeAsUpEnabled(true)
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_displaymessage); getSupportActionBar().setDisplayHomeAsUpEnabled(true); // If your minSdkVersion is 11 or higher, instead use: // getActionBar().setDisplayHomeAsUpEnabled(true); }
这样一来,点击图标左边的箭头,就会退出该activity。
定制你的ActionBar
从Android 3.0开始,android原生提供了一些适用actionbar的主题,Theme.Holo家庭。如果你要支持Android 2.1版本开始的,就要用Theme.AppCompat家庭。使用主题
3.0以上,你可以使用:Theme.Holo
Theme.Holo.Light
Theme.Holo.Light.DarkActionBar
使用方式,可以定义在application下或者activity下,如下:
<application android:theme="@android:style/Theme.Holo.Light" ... />
支持2.1以上的主题如下:
Theme.AppCompat
Theme.AppCompat.Light
Theme.AppCompat.Light.DarkActionBar
定制背景
要改变ActionBar的背景,可以通过自定义主题,然后复写actionBarStyle属性。如果你使用的是navigation tabs或者是split action bar,你要使用到backgroundStacked和backgroundSplit这两个属性。
注意一点的是,自定义主题时,一定要继承自上面介绍的这些主题,除非你想要把里面的所有原先存在的属性都推翻自己再定义一次。
3.0及以上
3.0以上版本,你可以像下面这样定制背景:res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar"> <item name="android:actionBarStyle">@style/MyActionBar</item> </style> <!-- ActionBar styles --> <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse"> <item name="android:background">@drawable/actionbar_background</item> </style> </resources>
然后在manifest里使用:
<application android:theme="@style/CustomActionBarTheme" ... />
2.1及以上
2.1以上版本,如下:res/values/themes.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.DarkActionBar"> <item name="android:actionBarStyle">@style/MyActionBar</item> <!-- Support library compatibility --> <item name="actionBarStyle">@style/MyActionBar</item> </style> <!-- ActionBar styles --> <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"> <item name="android:background">@drawable/actionbar_background</item> <!-- Support library compatibility --> <item name="background">@drawable/actionbar_background</item> </style> </resources>
这里可以看到,定义了两个版本。官方的建议是用support v7实现的,最好两个版本都定义,这样一来,如果你使用的手机是3.0以上的,v7库会自动使用android原生的actionbar,而不是v7库里实现的版本。
然后在activity或者application里使用
<application android:theme="@style/CustomActionBarTheme" ... />
定制文字颜色
要修改actionbar的文字颜色,你需要分别定义它的各种文字1.Action bar title:在actionBarStyle里复写titleTextStyle属性。
Note: The custom style applied to titleTextStyle should use TextAppearance.Holo.Widget.ActionBar.Title as the parent style.
2.Action bar tabs:在你的activity或application主题里,复写actionBarTabTextStyle
3.Action buttons:在你的activity或application主题里,复写actionMenuTextColor
3.0及以上
3.0及以上版本定义,如下:res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@style/Theme.Holo"> <item name="android:actionBarStyle">@style/MyActionBar</item> <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item> <item name="android:actionMenuTextColor">@color/actionbar_text</item> </style> <!-- ActionBar styles --> <style name="MyActionBar" parent="@style/Widget.Holo.ActionBar"> <item name="android:titleTextStyle">@style/MyActionBarTitleText</item> </style> <!-- ActionBar title text --> <style name="MyActionBarTitleText" parent="@style/TextAppearance.Holo.Widget.ActionBar.Title"> <item name="android:textColor">@color/actionbar_text</item> </style> <!-- ActionBar tabs text styles --> <style name="MyActionBarTabText" parent="@style/Widget.Holo.ActionBar.TabText"> <item name="android:textColor">@color/actionbar_text</item> </style> </resources>
2.1及以上
2.1及以上版本,定义如下:res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat"> <item name="android:actionBarStyle">@style/MyActionBar</item> <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item> <item name="android:actionMenuTextColor">@color/actionbar_text</item> <!-- Support library compatibility --> <item name="actionBarStyle">@style/MyActionBar</item> <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item> <item name="actionMenuTextColor">@color/actionbar_text</item> </style> <!-- ActionBar styles --> <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar"> <item name="android:titleTextStyle">@style/MyActionBarTitleText</item> <!-- Support library compatibility --> <item name="titleTextStyle">@style/MyActionBarTitleText</item> </style> <!-- ActionBar title text --> <style name="MyActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"> <item name="android:textColor">@color/actionbar_text</item> <!-- The textColor property is backward compatible with the Support Library --> </style> <!-- ActionBar tabs text --> <style name="MyActionBarTabText" parent="@style/Widget.AppCompat.ActionBar.TabText"> <item name="android:textColor">@color/actionbar_text</item> <!-- The textColor property is backward compatible with the Support Library --> </style> </resources>
定制tab背景指示
要修改actionbar的tab,复写activity或application主题下的actionBarTabStyle,然后这个style里再复写background属性。然后这个backgroud指定的drawable可以使用如下drawable:
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>
3.0及以上
3.0及以上版本,主题里做如下修改:res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@style/Theme.Holo"> <item name="android:actionBarTabStyle">@style/MyActionBarTabs</item> </style> <!-- ActionBar tabs styles --> <style name="MyActionBarTabs" parent="@style/Widget.Holo.ActionBar.TabView"> <!-- tab indicator --> <item name="android:background">@drawable/actionbar_tab_indicator</item> </style> </resources>
2.1及以上
2.1及以上版本做如下修改:res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat"> <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>
Actionbar悬浮
默认情况下,actionbar是布置在最上方的。它提供了我们可以显示隐藏的接口,show()和hide()。虽然这带来了方便,但也带来了性能上的问题。如果你不断的显示隐藏,那么布局就不断的在重新刷新,这显然是不合理的。如果遇到要不断显示隐藏actionbar,你可以增加一个悬浮属性,这样,actionbar就会浮在activity上,显示和隐藏就不会影响到activity重新布局了。
如果你要让actionbar半透明状态,只要设置背景半透明就行,比方说如下图的情况:
打开overlay属性
要打开悬浮属性,你需要自定义activity或者application主题,然后增加windowActionBarOverlay这个属性,设为true。3.0及以上
3.0及以上版本,如下所示:<resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@android:style/Theme.Holo"> <item name="android:windowActionBarOverlay">true</item> </style> </resources>
2.1及以上
2.1及以上版本,如下所示:<resources> <!-- the theme applied to the application or activity --> <style name="CustomActionBarTheme" parent="@android:style/Theme.AppCompat"> <item name="android:windowActionBarOverlay">true</item> <!-- Support library compatibility --> <item name="windowActionBarOverlay">true</item> </style> </resources>
指定layout top-margin
如果你要悬浮状态的actionbar看起来像是没悬浮,那就在activity的布局里指定个top margin或者top padding就行。3.0及以上
3.0及以上版本使用有android的:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="?android:attr/actionBarSize"> ... </RelativeLayout>
2.1及以上
2.1及以上版本使用没有android的:<!-- Support library compatibility --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="?attr/actionBarSize"> ... </RelativeLayout>
至此,actionbar的官方基础教程就介绍完了,下一篇Android ActionBar总结二继续讲述Actionbar更多功能。
相关文章推荐
- android如何播放资源文件夹raw中的视频
- 【android动态布局】之【ListView动态加载数据模板(使用xml布局)】
- Android Animation 动画Demo(Frame帧动画)
- 【android动态布局】之【ListView动态加载数据模板(使用xml布局)】
- android学习记录(1)——搭建环境(win x86)
- Android-Fragment(生命周期)
- android sdk 镜象站点
- android studio中图片格式的问题
- android 动画解析
- Android Xfermode 实战 实现圆形、圆角图片
- Android BitmapShader 实战 实现圆形、圆角图片
- Android笔记:调试android程序
- Android--多线程之Handler
- Android--多线程之Handler
- android开发 获取WIFI和有线的IP地址
- data binding——Android中的MVVM模式
- Android-Fragment(基本知识,静态加载,动态加载)
- Android模拟、实现、触发系统按键事件的方法
- android常见的 adb命令
- ANDROID之路-Android_参考资料_内置存储卡and外置存储卡