ActionBar
低版本和高版本用法不同

低版本:
1. 引用v7-appcompat
2. Activity继承ActionBarActivity
3. android:theme="@style/Theme.AppCompat.Light" >

高版本:
1. Activity自带actionBar
2.从Android3.0(APIlever11)开始,所有使用Theme.Holo主题(或者它的子类)的activity都包含了actionbar,当
targetSdkVersion或minSdkVersion属性被设置成“11”或更大时,它是默认主题。
所以,为你的activity添加actionbar,只需简单地设置属性为11或者更大。
 
常用操作:
搜索 
Actionbar允许你为当前上下文中最重要的操作添加按钮。那些直接出现在actionbar中的icon和/或文本被称作action
buttons(操作按钮)。安排不下的或不足够重要的操作被隐藏在actionoverflow中。
1.所有的操作按钮和actionoverflow中其他可用的条目都被定义在菜单资源的XML文件中。通过在项目的res/menu目录中
新增一个XML文件来为actionbar添加操作。(V7、V4只是功能功能不一样,没有升级的说法,要兼容低版本就要全部导V7的包,另外需要自定义命名空间)
  1. <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:actionViewClass="android.support.v7.widget.SearchView"
    yourapp:showAsAction="ifRoom"/>
    <!-- 设置, 在溢出菜单中展示 -->
    <item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:showAsAction="never" /> </menu>
2.写监听(错误可以忽略,判断下就可以了)
  1. @SuppressLint("NewApi")
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    // 如果运行的环境 (部署到什么版本的手机 )大于3.0
    if (android.os.Build.VERSION.SDK_INT > 11) {
    SearchView searchView = (SearchView) menu.findItem(
    R.id.action_search).getActionView();
    searchView.setOnQueryTextListener(this);// 搜索的监听
    }
    return true;
    }

     

3.处理actionBar菜单条目的点击事件
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_search) {
Toast.makeText(getApplicationContext(), "搜索", 0).show();
}
return drawerToggle.onOptionsItemSelected(item)|super.onOptionsItemSelected(item);
}
// 当搜索提交的时候
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getApplicationContext(), query, 0).show();
return true;
}
// 当搜索的文本发生变化
@Override
public boolean onQueryTextChange(String newText) {
return true;
}
}

  

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// 处理动作按钮的点击事件
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
eturn true;
default:
return ;
super.onOptionsItemSelected(item);
}
}
 
返回按钮的处理 
在不是主要入口的其他所有屏中(activity不位于主屏时),需要在actionbar中为用户提供一个导航到逻辑父屏的up
button(向上按钮)。

1.在另一个activity
  1. 	protected void initActionBar() {
    super.initActionBar();
    ActionBar actionBar = getSupportActionBar();//通过这样或得actionbar
    actionBar.setDisplayHomeAsUpEnabled(true);
    // 如果你的minSdkVersion属性是11活更高, 应该这么用:
    // getActionBar().setDisplayHomeAsUpEnabled(true);
    }
2.清单文件中指定它的父亲(高版本就不需要写元数据了)
  1.   <activity  android:name=".DetailActivity"
    android:label="@string/app_detail"
    android:parentActivityName="com.itheima.googleplay.MainActivity"
    >
    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value="com.itheima.googleplay.MainActivity" />
    </activity>
实现ActionBar  Tab标签
 1  在Drawable 目录下 写了一个标签的状态选择器(具体查文档)
  1. <!--	按钮没有按下的状态	-->
    <!-- 没有焦点的状态 -->
    <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" />
    <!-- 有焦点的状态 (例如D-Pad控制或者鼠标经过) -->
    <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" />
    <!-- 按钮按下的状态D -->
    <!-- 没有焦点的状态 -->
    <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" />
    <!--有焦点的状态 (例如D-Pad控制或者鼠标经过)-->
    <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  实现自定义主题 (想改的话改图片就行了)
  1. 	<?xml version="1.0" encoding="utf-8"?>
    <resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
    parent="@style/Theme.AppCompat.Light">
    //这里会报错,写着只有11以上能能用,可以先在清单文件中改成11,然后在改成低的就不报错了
    <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>
 3  在代码里添加标签(删除、隐藏等操作改模式就行)
  1. 		ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    Tab tab1=actionBar.newTab().setText("标签一").setTabListener(new MyTabListener());//需要这个接口,可以什么也不写
    actionBar.addTab(tab1);
DrawerLayout 
在布局里这样写就行了
  1. <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/dl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" > <!--如果抽屉没有打开 会显示线性布局 -->
    <LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" /> </LinearLayout>
    <!-- 左面可以滑出来一个抽屉 -->
    <!--
    <RelativeLayout
    android:layout_gravity="left"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"> </RelativeLayout> -->
    <RelativeLayout
    android:layout_gravity="right"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00"> </RelativeLayout>
    </android.support.v4.widget.DrawerLayout>

      可以一进如程序就打开一个抽泣

  1. 	drawerLayout=(DrawerLayout) findViewById(R.id.dl);
    drawerLayout.openDrawer(Gravity.RIGHT);// 打开左面抽屉

      

ActionBarDrawerToggle  
  控制抽屉的开关, 显示在actionBar 上面 
		ActionBar actionBar = getSupportActionBar();//如果是高版本直接getActionBar
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
1)显示Navigation Drawer的 Activity 对象
2) DrawerLayout 对象
3)一个用来指示Navigation Drawer的 drawable资源
4)一个用来描述打开Navigation Drawer的文本 (用于支持可访问性)。
5)一个用来描述关闭Navigation Drawer的文本(用于支持可访问性). drawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout, R.drawable.ic_drawer_am, R.string.open_drawer,
R.string.close_drawer){
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
Toast.makeText(getApplicationContext(), "抽屉关闭了", 0).show();
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
Toast.makeText(getApplicationContext(), "抽屉打开了", 0).show();
} };
mDrawerLayout.setDrawerListener(drawerToggle);
// 让开关和actionbar建立关系
drawerToggle.syncState();
/** 处理actionBar菜单条目的点击事件 */
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_search) {
Toast.makeText(getApplicationContext(), "搜索", 0).show();
} return drawerToggle.onOptionsItemSelected(item)|super.onOptionsItemSelected(item);
}

  

切换ViewPager 
actionbar的Tab不好看,可以用viewpager里的Tab,只需要嵌套这个PagerTabStrip就可以了
如果的viewpager里是fragment,可以继承FragmentActivity 

  1. 	    <android.support.v4.view.ViewPager
    android:id="@+id/vp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    >
    <android.support.v4.view.PagerTabStrip //这样viepager上面就有了Tab
    android:id="@+id/pager_tab_strip“
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="#ffffff"
    android:textColor="#000"
    android:paddingTop="4dp"
    android:paddingBottom="4dp" /> </android.support.v4.view.ViewPager>
    private class MainAdpater extends FragmentStatePagerAdapter{
    public MainAdpater(FragmentManager fm) {
    super(fm);
    }
    // 每个条目返回的fragment
    // 0
    @Override
    public Fragment getItem(int position) {
    if(position==0){
    return new HomeFragment();
    }else{
    return new AppFragment();//现在先这样写,其余的都显示这个fragment
    }
    }
    // 一共有几个条目
    @Override
    public int getCount() {
    return 4;
    }
    // 返回每个条目的标题
    @Override
    public CharSequence getPageTitle(int position) {
    return "标签"+position;
    } }

      

    完整代码请看:2.代码抽取

 

1.ActionBar的更多相关文章

  1. Android中通过ActionBar为标题栏添加搜索以及分享视窗

    在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果.Action ...

  2. Android 添加ActionBar Buttons

    一.在res/menu文件夹下创建Xml文件 跟标签为menu,设置item <?xml version="1.0" encoding="utf-8"?& ...

  3. mono for android 自定义titleBar Actionbar 顶部导航栏 修改 样式 学习

    以前的我是没有做笔记的习惯的,学习了后觉得自己能记住,但是最近发现很多学的东西都忘记了,所有现在一有新的知识,就记下来吧. 最近又做一个mono for android 的项目 这次调整比较大,上次做 ...

  4. Xamarin.Android之ActionBar与菜单

    一.选项卡 如今很多应用都会使用碎片以便在同一个活动中能够显示多个不同的视图.在Android 3.0 以上的版本中,我们已经可以使用ActionBar提供的Tab来实现这种效果,而不需要我们自己去实 ...

  5. 自定义ActionBar标题与菜单中的文字样式

    自定义标题文字样式 标题样式是ActionBar样式的一部分,所以要先定义ActionBar的样式 <style name="AppTheme" parent="A ...

  6. ActionBar设置自定义setCustomView()留有空白的问题

    先来看问题,当我使用ActionBar的时候,设置setCustomView时,会留有空白的处理 网上很多朋友说可以修改V7包到19,结果处理的效果也是不理想的. 下面贴出我觉得靠谱的处理代码 pub ...

  7. Android ActionBar 初探

    1.指南,例子,个人感觉 首先上官网指南链接http://developer.android.com/guide/topics/ui/actionbar.html 参考了官网上的例子http://de ...

  8. Menu与ActionBar的爱恨情仇

    最近在开发一款音乐播放器,在开发过程中遇到了一点小麻烦,通过android API搞清楚了Menu与ActionBar的爱恨情仇,写了个小Demo祭奠一下那些年我们陷进去的坑,有不对的地方请大神们批评 ...

  9. ANDROID中去掉ACTIONBAR或TABWIDGET的分隔线

    在android中,有时需要对ActionBar或者TabWidget的分隔线进行定制,如取消,相关的属性设置为android:divider 以TabWidget为例,取消对应的函数: tabWid ...

  10. 关于ActionBar

    添加ActionBar: Android 3.0(API 11)(不含API11)以下的版本中,如果需要活动有ActionBar,需要让活动继承ActionBarActivity类,并且在Manife ...

随机推荐

  1. spring Mongodb查询索引报错 java.lang.NumberFormatException: empty String

    最近事情比较多,本篇文章算是把遇到的问题杂糅到一起了. 背景:笔者最近在写一个mongo查询小程序,由于建立索引时字段名用大写,而查询的时候用小写. 代码如下: db.getCollection(&q ...

  2. android 7.0 调用系统相机崩溃的解决方案(非谷歌官方推荐)

    解决方案: 1.(推荐)7.0之后你的app就算有权限,给出一个URI之后手机也认为你没有权限. 不用修改原有代码,在Application的oncreate方法中:(或者直接放在调用相机的activ ...

  3. Security.ssl-pinning

    SSL Pinning 1. What's SSL Pinning? "SSL Pinning is making sure the client checks the server’s c ...

  4. vue中提示$index is not defined

    今天学习Vue中遇到了一个报错信息:$index is not defined,是我写了个for循环在HTML中,然后是因为版本的问题 下面是解决方法: 原来的是 v-for="person ...

  5. 修改pudb颜色

    2019-02-19,18点20vim调整颜色vim ~/.vimrc 这个pudb的配色用上的方法改不了.调试状态时候按o和回车能切换console和调试界面. 成功了.通过修改pudb源代码来实现 ...

  6. 详解js跨域

    什么是跨域? 概念:只要协议.域名.端口有任何一个不同,都被当作是不同的域. 对于端口和协议的不同,只能通过后台来解决.URL 说明 是否允许通信 http://www.a.com/a.js http ...

  7. 【Java】基本数据类型

    基本知识点给个链接: https://blog.csdn.net/qwe969153746/article/details/53353534 问题: 1.3*0.1 == 0.3 返回什么: fals ...

  8. 动态库的链接和链接选项-L,-rpath-link,-rpath

    https://my.oschina.net/shelllife/blog/115958 链接动态库 如何程序在连接时使用了共享库,就必须在运行的时候能够找到共享库的位置.linux的可执行程序在执行 ...

  9. vue中created、mounted、 computed,watch,method 等方法整理

    created:html加载完成之前,执行.执行顺序:父组件-子组件 mounted:html加载完成后执行.执行顺序:子组件-父组件 methods:事件方法执行 watch:watch是去监听一个 ...

  10. select下拉框左右变换

    效果图: 使用jQuery插件---multiselect2side做法: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ...