1.ActionBar
ActionBar
低版本和高版本用法不同
1. 引用v7-appcompat
2. Activity继承ActionBarActivity
3. android:theme="@style/Theme.AppCompat.Light" >
2.从Android3.0(APIlever11)开始,所有使用Theme.Holo主题(或者它的子类)的activity都包含了actionbar,当
<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>
@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;
}
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);
}
}
protected void initActionBar() {
super.initActionBar();
ActionBar actionBar = getSupportActionBar();//通过这样或得actionbar
actionBar.setDisplayHomeAsUpEnabled(true);
// 如果你的minSdkVersion属性是11活更高, 应该这么用:
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
<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>
<!-- 按钮没有按下的状态 -->
<!-- 没有焦点的状态 -->
<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>
<?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>
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab1=actionBar.newTab().setText("标签一").setTabListener(new MyTabListener());//需要这个接口,可以什么也不写
actionBar.addTab(tab1);
<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>可以一进如程序就打开一个抽泣
drawerLayout=(DrawerLayout) findViewById(R.id.dl);
drawerLayout.openDrawer(Gravity.RIGHT);// 打开左面抽屉
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);
}
<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的更多相关文章
- Android中通过ActionBar为标题栏添加搜索以及分享视窗
在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果.Action ...
- Android 添加ActionBar Buttons
一.在res/menu文件夹下创建Xml文件 跟标签为menu,设置item <?xml version="1.0" encoding="utf-8"?& ...
- mono for android 自定义titleBar Actionbar 顶部导航栏 修改 样式 学习
以前的我是没有做笔记的习惯的,学习了后觉得自己能记住,但是最近发现很多学的东西都忘记了,所有现在一有新的知识,就记下来吧. 最近又做一个mono for android 的项目 这次调整比较大,上次做 ...
- Xamarin.Android之ActionBar与菜单
一.选项卡 如今很多应用都会使用碎片以便在同一个活动中能够显示多个不同的视图.在Android 3.0 以上的版本中,我们已经可以使用ActionBar提供的Tab来实现这种效果,而不需要我们自己去实 ...
- 自定义ActionBar标题与菜单中的文字样式
自定义标题文字样式 标题样式是ActionBar样式的一部分,所以要先定义ActionBar的样式 <style name="AppTheme" parent="A ...
- ActionBar设置自定义setCustomView()留有空白的问题
先来看问题,当我使用ActionBar的时候,设置setCustomView时,会留有空白的处理 网上很多朋友说可以修改V7包到19,结果处理的效果也是不理想的. 下面贴出我觉得靠谱的处理代码 pub ...
- Android ActionBar 初探
1.指南,例子,个人感觉 首先上官网指南链接http://developer.android.com/guide/topics/ui/actionbar.html 参考了官网上的例子http://de ...
- Menu与ActionBar的爱恨情仇
最近在开发一款音乐播放器,在开发过程中遇到了一点小麻烦,通过android API搞清楚了Menu与ActionBar的爱恨情仇,写了个小Demo祭奠一下那些年我们陷进去的坑,有不对的地方请大神们批评 ...
- ANDROID中去掉ACTIONBAR或TABWIDGET的分隔线
在android中,有时需要对ActionBar或者TabWidget的分隔线进行定制,如取消,相关的属性设置为android:divider 以TabWidget为例,取消对应的函数: tabWid ...
- 关于ActionBar
添加ActionBar: Android 3.0(API 11)(不含API11)以下的版本中,如果需要活动有ActionBar,需要让活动继承ActionBarActivity类,并且在Manife ...
随机推荐
- linux '--stdin'错误 -批量修改密码
虚拟机:VMware虚拟机 系统:Linux ubuntu 4.4.0-31-generic #50~14.04.1-Ubuntu SMP Wed Jul 13 01:07:32 UTC 2016 ...
- JavaScript中Object.keys用法
Object.keys()方法会返回一个由一个给定对象的自身可枚举属性组成的数组. var data={a:1,b:2,c:9,d:4,e:5}; console.log(data);//{a: 1, ...
- 手机号读取城市数据库2018年3月excel版
EXCEL表中是更新到2018年3月份的手机归属地数据库. 手机号读取城市数据库2018年3月excel版.zip
- 使用mobx项目开发总结(不再更新)
mobx的优点 1,使用@observer的组件真正实现按需更新,只有监听的数据发生变化,它才会re-render,尽管父组件发生更新,但是子组件只要有@observer,则不会触发更新,类似于实 ...
- Linux环境(Centos7)下部署.NetCore2.0的Web应用
Web应用基于Windows环境下开发,然后部署到Linux 1.进入VS2017,点击新建->项目->.NetCore->ASP.NET Core Web应用程序,确定 2.选择W ...
- abaqus修改inp直接建立工程
前面已经知道,通过修改以下inp的节点和单元编号,就可以新建模型,可是对于大的工程来说,逐个选取单元进行添加材料以及确定哪步进行填土仍是比较麻烦的(如果工程网格划分好并告知哪些单元好属于哪些材料,哪些 ...
- DOTween的基本用法
首先声明一点,不要简单的认为 DOTween 只能用在 Transform 组件上完成一些简单的动画,或者是完成一些 UI 动画,DOTween 的用途是很广的,unity中有很多组件都可以使用 DO ...
- PDF分享:国外优秀数学教材选评
<国外优秀数学教材选评>推荐书目下载 具体内容请查看原内容: http://www.library.fudan.edu.cn/wjzx/list/373-1-20.htm 或者http:/ ...
- Https,Http,TCP,IP的一些理解
网络模型分为7层,应用层,表现层,会话层,传输层,网络层,链路层,物理层,每一层有很多不同的协议. http:属于应用层的协议,负责的是数据以什么结构传输也可以说成是打包成什么样子 SSL/TLS:属 ...
- numpy 与 matplotlib 的应用
numpy 与 matplotlib 的应用 一.库函数介绍 1. numpy库 NumPy(Numeric Python)提供了一个N维的数组类型ndarray,Numpy底层使用C语言编写,内部解 ...