Android学习之ActionBar(2)
来源:互联网 发布:python数据预处理 编辑:程序博客网 时间:2024/05/24 02:40
LearnActionBar
1.启用Action Bar
2.在Action Bar上添加按钮
3.自定义Action Bar样式
4.自动隐藏Action Bar
5.Action Provider的使用
6.ActionBarSherlock的使用
1.启用Action Bar
2.在Action Bar上添加按钮
android 3.0和以上版本 1) android:showAsAction="always" 一直显示在Action Bar上 2)android:showAsAction="ifRoom" 如果Action Bar空间足够,则显示 3) android:showAsAction="never" 不显示在Action Bar中,折叠在OverFlow里 4) android:showAsAction="withText" 菜单项和它的图标,菜单文本一起显示android 2.1和以上版本 1) 在menu.xml中需要加入自定义命名空间 xmlns:yourapp="http://schemas.android.com/apk/res-auto" 2) 在showAsAction属性前指定命名空间 yourapp:showAsAction="ifRoom"1.创建menu.xml文件<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="Search" app:showAsAction="ifRoom" /> <item android:id="@+id/action_setting" android:title="Setting" app:showAsAction="never" /></menu>2.MainActivity.java@Overridepublic boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu, menu); return true;}3.添加点击事件@Overridepublic boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_search: Toast.makeText(MainActivity.this, "action_search", Toast.LENGTH_SHORT).show(); break; case R.id.action_setting: Toast.makeText(MainActivity.this, "action_setting", Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item);}4.向上按钮和返回按钮向上按钮是父子关系返回按钮是前后关系<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(兼容4.0及以下) --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.myfirstapp.MainActivity" /> </activity></application>//设置按钮可见@Overridepublic 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,不知道为什么?
3.自定义Action
1.如果你使用的是android版本4.0及以上版本,你可以使用android系统的主题android:theme="@android:style/Theme.Holo.Light.DarkActionBar"2.如果你使用的是android版本2.1及以上版本,你可以使用support v7库中的主题android:theme="@style/Theme.AppCompat.Light.DarkActionBar"3.自定义ActionBar背景 1.在themes.xml中新建自定义Style,使其继承已有的Action Bar Style(Theme.Holo) 2.覆盖其actionBarStyle属性 3.actionBarStyle属性值指向另一个已被覆写了background属性的Style 4.指定该background的属性值 android 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> <application android:theme="@style/CustomActionBarTheme" ... /> android 2.1及以上版本实现(support v7): 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> <application android:theme="@style/CustomActionBarTheme" ... />4.自定义ActionBar文本颜色,可以参照以上自定义背景的做法(google官方文档)5.自定义ActionBar Tab背景,可以参照以上自定义背景的做法(google官方文档)
4.自动隐藏Action Bar
以上的工程使用的是support v7库,也就是2.1及以上版本,现在开始一个新的工程使用的是3.0及以上版本,以下都是针对3.0及以上版本实现。当前工程:LearnActionBarOverlay1.操作栏启用叠加模式,您需要创建一个定制主题扩展现有的操作栏主题和设置android:windowActionBarOverlay属性为true。 1.修改styles.xml文件,新建 <style name="CustomActionBarOverlay" parent="android:Theme.Holo"> <item name="android:windowActionBarOverlay">true</item> </style> 2.修改AndroidManifest.xml文件 android:theme="@style/CustomActionBarOverlay"
2.指定布局顶边
在覆盖模式下操作栏时,它可能会掩盖你的一些布局,应该保持可见。以确保这些物品仍低于操作栏,使用actionBarSize指定填充的高度,使主布局内容显示在Actionbar的下面。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="?android:actionBarSize"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/jikexueyuan" /> </RelativeLayout>
5.Action Provider的使用
1.创建menu.xml<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_share" android:showAsAction="ifRoom" android:title="@string/action_share" android:actionProviderClass="android.widget.ShareActionProvider"/> <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:showAsAction="ifRoom" android:title="@string/action_search" /> <item android:id="@+id/action_setting" android:showAsAction="never" android:title="@string/action_setting" /></menu>2.android3.0及以上版本<itemandroid:id="@+id/action_share"android:showAsAction="ifRoom"android:title="@string/action_share"android:actionProviderClass="android.widget.ShareActionProvider"/>@Overridepublic boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu, menu); MenuItem shareItem = menu.findItem(R.id.action_share); shareActionProvider = (ShareActionProvider) shareItem.getActionProvider(); shareActionProvider.setShareIntent(getDefaultIntent()); return true;}private Intent getDefaultIntent() { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("image/*"); return intent;}android2.1及以上版本<item...yourapp:actionProviderClass="android:support.v7.widget.ShareActionProvider"/>shareItem = menu.findItem(R.id.action_share);MenuItemCompat.getActionProvider(shareItem);
6.ActionBarSherlock的使用
github地址:https://github.com/JakeWharton/ActionBarSherlock/tree/master/actionbarsherlock
7.源码下载
https://github.com/zheng-zy/AndroidStudioProjects.git
0 0
- Android学习之ActionBar(2)
- Android之ActionBar学习
- Android之ActionBar学习
- Android之ActionBar学习
- Android之ActionBar学习
- Android之ActionBar学习
- Android之ActionBar学习
- Android之ActionBar学习
- Android之ActionBar学习
- Android之ActionBar学习
- Android之ActionBar学习
- Android学习之ActionBar
- Android之ActionBar学习 (译)
- Android学习之ActionBar 一
- 学习Android从0开始之ActionBar(活动条)
- Android学习之 ActionBar 和 SherlockActionBar
- Android开发学习之初识ActionBar
- Android 学习笔记二十三之 ActionBar
- EHCI和OHCI,UHCI的比较和区别
- Ubuntu 14.04安装PostgreSQL How To Install and Use PostgreSQL on Ubuntu 14.04
- c++ typedef使用
- HDU 2215 Maple trees(凸包-Graham扫描法+最小圆覆盖)
- rebase -i 汇合提交
- Android学习之ActionBar(2)
- MBCS与Unicode的转换
- 深入理解 AngularJS 的 Scope
- 再session中保存用户登录信息
- 关于overflow
- elasticsearch 如何能像数据库一样,按条件精准查询
- 入门视频采集与处理
- U_BOOT_CMD
- 【job】面试中常见的笔试梳理