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. 使用sublime遇到的问题汇总

    问题一:用GBK编码的文件用Sublime打开出现中文乱码的解决办法: 通过ctrl+shift+p--package control install安装插件"ConvertToUTF8&q ...

  2. 使用cordova插件barcodescanner遇到的坑

    最近接手了一个app任务,由于不懂android和ios,只想简单点写代码,所以,最终采用了基于H5的web框架:ionic + cordova(也叫phonegap)来开发.app的设计中有一个扫描 ...

  3. create dll project based on the existing project

    Today, I have to create a dll project(called my.sln), the dllmain.cpp/.h/ is already in another proj ...

  4. 用JS控制video暂停再播放

    模块就是用来进行封装,进行高内聚 低耦合的功能.其实各人认为ng2 的模块和.net的工程类似,如果要使用模块中定义的功能,第一步就是必须要引用它,ng2 中叫import 导入.那么我们看模块是否有 ...

  5. Codeforces Round #379 (Div. 2) E. Anton and Tree

    题意: 给一颗树 每个节点有黑白2色 可以使一个色块同事变色,问最少的变色次数. 思路: 先缩点 把一样颜色的相邻点 缩成一个 然后新的树 刚好每一层是一个颜色. 最后的答案就是树的直径/2 不过我用 ...

  6. LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  7. 在指定的div中搜索内容,并滚动显示到当前搜索到的内容处

    我想要的是页面中有个带滚动条的div对象,里面有很多内容,想要用js搜索到div中的某个字符串内容,然后将div的滚动条滚动到搜索到的内容处显示,自动定位.先是查找页面中的内容,然后将找到的内容创建t ...

  8. SSR三网免流一键包以及使用教程

    这篇文章只是传统的SSR免流教程 首先拿到一台vps,可以是centos,可以是debian,也可以是ubuntu 使用xshell连接vps 执行一键安装包 wget http://104.224. ...

  9. Javascript 技巧集(1)

    1. 数组克隆, 使用 slice() 方法 var a1 = [1,2,3,4]; var a2 = a1.slice(); 2. 强制将变量值转化为 bool 类型,前置双感叹号 !! var a ...

  10. Tplink客户端设置

    之前在JD上面买了个Tplink,将公司的无线网转成有线的给我的台式机用,可是突然就掉线了,怎么配置都不行.甚至按向导去做了,后来连配置界面都进不去.然后又再某宝上面买了两个,回来配置也发现了这个情况 ...