Android中的标题栏
来源:互联网 发布:民办学校前景知乎 编辑:程序博客网 时间:2024/06/18 04:00
1、标题栏的历史
2、标题栏的显示和隐藏
3、使用Toolbar
Theme设置为NoActionBarLayout界面中添加Toolbar代码中调用setSupportActionBar(Toolbar)
代码布局
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"/>
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); private void setToolbar(Toolbar toolbar) { setSupportActionBar(toolbar); toolbar.setTitle("主标题"); toolbar.setSubtitle("副标题"); toolbar.setLogo(R.drawable.logo); toolbar.inflateMenu(R.menu.action_menu); toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { return false; } }); }
4、ActionBar的介绍和使用
(1)显示Logo
在Theme.AppCompat情况下显示的代码
ActionBar actionBar = getSupportActionBar(); actionBar.setLogo(R.drawable.logo); actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayUseLogoEnabled(true); actionBar.setSubtitle("副标题");
(2)显示标题
(3)添加按钮
示例代码
public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.action_menu, menu); return super.onCreateOptionsMenu(menu);}
布局代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/search_icon" android:icon="@drawable/actionbar_search_dark_icon" android:title="@string/search" app:actionViewClass="android.support.v7.widget.SearchView" app:showAsAction="ifRoom|collapseActionView"> </item> <item android:id="@+id/plus" android:icon="@drawable/actionbar_plus_icon_normal" android:title="@string/more" app:actionProviderClass="android.support.v7.widget.ShareActionProvider" app:showAsAction="always"> </item> <item android:id="@+id/more" android:icon="@drawable/actionbar_plus_icon_normal" android:title="@string/more" app:actionProviderClass="com.netease.study.ui.title.PlusProvider" app:showAsAction="always"> </item></menu>
(4)showAsAction的显示规则
always永远显示ActionBar中,如果空间不够则无法显示 ifRoom表示有空间的时候显示在ActionBar中,否在显示在overflow中 never一直显示在overflow中
(5)Action按钮的响应
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case android.R.id.home: break; } return super.onOptionsItemSelected(item); }
(6)ActionView
xml文件
app:actionViewClass="android.support.v7.widget.SearchView"
代码设置
SearchView searchView = (SearchView) MenuItemCompat.getActionView(search); //设置点击事件 MenuItemCompat.setOnActionExpandListener(search, new MenuItemCompat.OnActionExpandListener() { @Override public boolean onMenuItemActionExpand(MenuItem item) { return false; } @Override public boolean onMenuItemActionCollapse(MenuItem item) { return false; } });
(7)ActionProvider
xml文件
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
MenuItem item = menu.findItem(R.id.plus);ShareActionProvider provider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);provider.setShareIntent(getShareIntent());public Intent getShareIntent() { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("image/*"); return intent;}
(8)开启页面导航
getSupportActionBar().setDisplayHomeAsUpEnabled(true)
public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ //统一返回同一个页面 case android.R.id.home: Intent upIntent = NavUtils.getParentActivityIntent(this); if(upIntent!=null){ upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); NavUtils.navigateUpTo(this,upIntent); }else{ finish(); } break; }}
(9)ActionMode
弹出一个Menu
open.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { ActionBarActivity.this.startActionMode(new ActionMode.Callback() { @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { mode.getMenuInflater().inflate(R.menu.action_menu1, menu); return true; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false; } @Override public void onDestroyActionMode(ActionMode mode) { } }); return true; } });
小技巧:
tools:showIn :在当前编写界面就可以看到效果
tools:text :在当前预览界面可以看到效果,编译后不存在
<LinearLayout 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:orientation="vertical" tools:context="com.netease.study.ui.title.CustomTitleActivity"> <include layout="@layout/title_layout1"></include> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内容"/></LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:background="@color/color_FA5864" tools:showIn="@layout/activity_custom_title" android:layout_height="56dp"> <ImageView android:id="@+id/back_icon" android:layout_width="wrap_content" android:layout_centerVertical="true" android:src="@drawable/actionbar_white_back_icon" android:layout_height="wrap_content"/> <TextView android:layout_toRightOf="@id/back_icon" android:layout_width="wrap_content" tools:text="答一答" android:textSize="16sp" android:textColor="@color/white" android:layout_centerVertical="true" android:layout_height="wrap_content"/> <ImageView android:layout_alignParentRight="true" android:layout_width="wrap_content" android:src="@drawable/person_center" android:layout_centerVertical="true" android:padding="18dp" android:layout_height="wrap_content"/></RelativeLayout>
0 0
- Android studio中的标题栏
- Android中的标题栏
- 去掉android 项目中的标题栏
- 去掉android应用中的系统标题栏
- 如何将Android中的标题栏自定义
- Android 标题栏
- Android 标题栏
- android标题栏
- android 去掉标题栏、无标题栏
- 隐藏Android模拟器中的标题栏和电池等图标
- Android中的标题栏不显示问题的解决
- 去除activity中的标题栏
- Android 删除标题栏或自定义标题栏注意事项
- Android隐藏标题栏
- android 隐藏标题栏
- android自定义标题栏progressBar
- Android标题栏进度条显示
- Android自定义标题栏
- 学习数据库封装(十)medoo数据类
- 今目标让生活简单起来
- SDN之QoS--2:系统介绍以及主要模块(Floodlight)
- 【转】Fiddler 抓包工具总结
- java 代码风格
- Android中的标题栏
- javaweb开发过程中小工具系列之返回一个不重复的字符串
- android应用市场包名
- 深度学习框架Caffe源码解析(概览)
- python中的字符串常用函数
- MySQL函数大全及用法示例
- UITouch
- c::关于strcpy的几道经典的面试题~
- 什么是TCP/IP协议