Android 自带的toolbar 往往不能很好的的满足我们的个性化要求。因此我们经常使用自定的的标题栏。而Android系统本身也允许我们自定以标题栏。

记录一下,自定义标题栏常遇到的问题。先上效果图:

实现起来也很简单。在Activity 的 setContentView方法前添加

  requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

其含义是允许用户自定义标题栏。

然后再在setContentView后添加

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_navigation_bar);
其中 R.layout.custom_navigation_bar 为我们自定义的标题栏样式,
 <?xml version="1.0" encoding="utf-8"?>
<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="44dp"
android:orientation="vertical"
tools:context=".BaseActivity"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="10dp"> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left|center_vertical"
android:paddingBottom="2dp"
android:paddingTop="2dp"> <ImageView
android:id="@+id/navigation_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@mipmap/rt_tianjiaguanzhu" /> <TextView
android:id="@+id/navigation_left_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="散文"
android:textSize="16sp" /> </LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="2dp"
android:paddingTop="2dp"> <TextView
android:id="@+id/navigation_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:text="全部关注"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="16sp" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/icon_down" />
</LinearLayout> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right|center_vertical"
android:paddingBottom="2dp"
android:paddingTop="2dp"> <ImageView
android:id="@+id/navigation_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:src="@mipmap/settings" />
</LinearLayout> </LinearLayout> <View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/lightgray" /> </LinearLayout>

最后设置主题,也只最容易出错的地方。对使用该方式添加标题栏的Activity 添加主题样式。

这是我的主题样式 应继承  android:Theme.Light 主题否则会报错。

     <!-- 标题栏样式 -->
<style name="WindowTitleBackground" >
<item name="android:background">@color/colordarkgray</item>
</style>
<style name="MyTheme" parent="android:Theme.Light">
<item name="android:windowTitleSize">45dp</item>
<item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
</style>

Activity 引用代码如下。

android:theme="@style/MyTheme" 

这样就可以实现自定义的效果了。

补充:上面说主题不继承 android:Theme.Light 会报错。查了一下是标题栏冲突的原因。
    我们只需要在继承的主题里添加如下属性就可以了。
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">false</item>
这样我们就可以继承别的主题使用一些高大上的效果了。如 Theme.AppCompat主题等。 有朋友问我,显示这个标题的时候同时也显示了系统自带的标题是什么原因,查看代码后发现他的Activity继承了AppCompatActivity 这里我们应继承Activity 就好了。

												

setFeatureInt、android 自定义标题栏的更多相关文章

  1. [置顶] xamarin android自定义标题栏(自定义属性、回调事件)

    自定义控件的基本要求 这篇文章就当是自定义控件入门,看了几篇android关于自定义控件的文章,了解了一下,android自定义控件主要有3种方式: 自绘控件:继承View类,所展示的内容在OnDra ...

  2. Android 自定义标题栏

    开发 Android APP 经常会用到自定义标题栏,而有多级页面的情况下还需要给自定义标题栏传递数据. 本文要点: 自定义标题填充不完整 自定义标题栏返回按钮的点击事件 一.代码 这里先介绍一下流程 ...

  3. Android -- 自定义标题栏,背景颜色填充满

    设置标题栏背景 1> 准备背景图片: background_pix.png 注:用背景图片比用颜色好处,可以让背景看起来有凹凸感. 2> drawable文件夹下放xml文件 bitmap ...

  4. (转)Android 自定义标题栏(title栏)

    转:http://blog.csdn.net/jamin0107/article/details/6715678 第一步,向实现自定义标题栏,需要在onCreate方法里这样写 requestWind ...

  5. android 自定义标题栏 titleBar自定义

    在value文件夹下添加style.xml <?xml version="1.0" encoding="utf-8"?> <resources ...

  6. Android—自定义标题栏的实现及遇见的问题解决

    开发者设计界面时候往往不会使用系统自带的标题栏,因为不美观,所以需要自己设置标题栏. 1.根据需求在xml文件中设置标题布局 <?xml version="1.0" enco ...

  7. Android自定义标题栏

    预览一下效果: 素材: 新建一个布局title_bar.xml,代码如下: <?xml version="1.0" encoding="utf-8"?&g ...

  8. Android开发-取消程序标题栏或自定义标题栏

    注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 在Android开发中,跟据需要我们有时候需要自定义应用程序的标题栏或者取消程序的标题栏,下面本菜鸟在此记录与分享一下自己使用的方法 ...

  9. Android应用开发基础篇(14)-----自定义标题栏

    一.概述 每一个应用程序默认的标题栏(注意与状态栏的区别)只有一行文字(新建工程时的名字),而且颜色.大小等都是固定的,给人的感觉比较单调.但当程序需要美化的时候,那么修改标题栏是就是其中一项内容,虽 ...

随机推荐

  1. 作业三:WC项目

    WC一个简单的文件检测系统 用c语言实现 不多说 上代码 #include<stdio.h> #include<stdlib.h> #include <io.h> ...

  2. 兼容ie7、8、9、10、FF、Chrome的遮罩显示

    经常碰到这种情形,要实现图片上有一层遮罩和按钮,鼠标滑过时遮罩颜色变深且按钮图片变化,磕磕碰碰终于弄出来题目所述兼容的解决方案. 对于遮罩的实现,将遮罩层.按钮.图片放置在同一个div中,根据abso ...

  3. 在MyEclipse和Eclipse中添加Hibernate开发工具

    一.插件准备 MyEclipse需要的插件:HibernateTools-3.2.4.zip Eclipse需要的插件:jbosstools-4.2.3.Final_2015-03-26_22-41- ...

  4. Spring源码学习之: 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  5. $.map和$.extend来复制数组(OA差旅)

    $.map:对数组的每个元素调用定义的回调函数并返回包含结果的数组. 对数组用指定的方法. //将原数组中每个元素加 4 转换为一个新数组. //jQuery 代码: $.map( [0,1,2], ...

  6. fastdfs 安装配置

      介绍文档   1.安装 # wget # tar xf download # cd FastDFS/ # ./make.sh 运行文件 有报错信息 compilation terminated. ...

  7. Yii2.0中form->field如何获取主表的一个字段并且设置为只读

    <?= $form->field($model, 'last_login_time')->textInput(['readonly' => 'true']) ?>

  8. <<Differential Geometry of Curves and Surfaces>>笔记

    <Differential Geometry of Curves and Surfaces> by Manfredo P. do Carmo real line Rinterval I== ...

  9. javascript判断某种元素是否进入可视区域

    判断是否在指定的可视区域内,先用最简单的方式,比如整个页面为可视区域 找到几个关键因素: sTop= $(window).scrollTop();  //滚动条距顶部的高度 clientHeight= ...

  10. canvas ---个性时钟

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...