转:http://blog.csdn.net/jamin0107/article/details/6715678

第一步,向实现自定义标题栏,需要在onCreate方法里这样写

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);

注意:

 requestWindowFeature要在setContentView之前

    etWindow().setFeatureInit最好在setContentView之后

第二步,就是写好自己的布局文件,实现标题栏的自定义。

不过我们会遇到一些问题,就是标题栏的高度不能自定义~下面就是解决办法~

下面,我们看一下android title标题栏 在源码 framework中的XML文件

screen_custom_title.xml

<LinearLayout android:orientation="vertical" android:fitsSystemWindows="true">
<FrameLayout android:id="@android:id/title_container"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_width="match_parent"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_height="?android:attr/windowTitleSize"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;style="?android:attr/windowTitleBackgroundStyle">
</FrameLayout>
<FrameLayout android:id="@android:id/content"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_width="match_parent"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_height="0dip"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_weight="1"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:foregroundGravity="fill_horizontal|top"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:foreground="?android:attr/windowContentOverlay"/>
</LinearLayout>

screen_title.xml

<LinearLayout android:orientation="vertical" android:fitsSystemWindows="true">

<FrameLayout android:layout_width="match_parent"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_height="?android:attr/windowTitleSize"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;style="?android:attr/windowTitleBackgroundStyle">
<TextView android:id="@android:id/title"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; style="?android:attr/windowTitleStyle"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:background="@null"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:fadingEdge="horizontal"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center_vertical"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="match_parent"/>
</FrameLayout>
<FrameLayout android:id="@android:id/content"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; android:layout_width="match_parent"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; android:layout_height="0dip"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; android:layout_weight="1"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:foregroundGravity="fill_horizontal|top"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:foreground="?android:attr/windowContentOverlay"/>
</LinearLayout>

既然是自定义标题,那我们就看screen_custom_title.xml,里面有一个title_container和一个content,组合成了标题栏,我们自定义标题所给出的view,都被content作为子view了,影响不了那个title_container和content,所以,任你怎么弄,它该留白的还是留白,你没招。
       看title_container有个style是这样的:

style="?android:attr/windowTitleBackgroundStyle"

content的foreground是这样的android:foreground="?android:attr/windowContentOverlay"

我们可以自己写一个style

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="CustomWindowTitleBackground">
<item name="android:background">#00cc00</item>
</style> <style name="title_bar" parent="android:Theme">
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
</resources>

加入到AndroidManifest

  <activity android:name=".CustomTitileBarActivity"
android:label="@string/app_name" android:theme="@style/title_bar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

PS:做的略显粗糙,不过以能实现功能为主

(转)Android 自定义标题栏(title栏)的更多相关文章

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

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

  2. Android 自定义标题栏

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

  3. setFeatureInt、android 自定义标题栏

    Android 自带的toolbar 往往不能很好的的满足我们的个性化要求.因此我们经常使用自定的的标题栏.而Android系统本身也允许我们自定以标题栏. 记录一下,自定义标题栏常遇到的问题.先上效 ...

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

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

  5. Android自定义标题栏

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

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

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

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

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

  8. 【起航计划 006】2015 起航计划 Android APIDemo的魔鬼步伐 05 App->Activity->Custom Title 自定义标题栏

    Android UI缺省的标题栏由android:label 定义,显示在屏幕左上角,Android允许Activity自定义标题栏,使用自定义Layout重新设置标题栏,比如实现Windows Mo ...

  9. android全屏去掉title栏的多种实现方法

    android全屏去掉title栏的多种实现方法 作者: 字体:[增加 减小] 类型:转载 时间:2013-02-18我要评论 android全屏去掉title栏包括以下几个部分:实现应用中的所有ac ...

随机推荐

  1. 2018-2-13-win10-edge扩展

    title author date CreateTime categories win10 edge扩展 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23 ...

  2. 新学MyBatis

    今天学习了Mybatis入门,将知识归纳一下: 创建一个java项目之后,想使用myBatis需要完成一下几个步骤: 1.先导jar包 2.写model文件 () 3.写全局配置文件 SqlMapCo ...

  3. visual studio 2013下搭建 安卓,ios,wp app开发平台

    1.安装 visual studio 2013 + Microsoft Visual Studio 2013 Update 4+Microsoft Build Tools 2015 2.安装java ...

  4. 链表vector

    根据逻辑次序的复杂程度,大致可以将各种数据结构划分为线性结构.半线性结构与非线性结构三大类. 在线性结构中,各数据项按照一个线性次序构成一个整体.最为基本的线性结构统称为序列(sequence),根据 ...

  5. IPv4 IPv6

    IP提供了一种best-effort,connection-less数据delivery服务. best-efforty意味着它不保证IP数据段会成功到达目的地.当error发生时,IP通常选择丢弃d ...

  6. dubbo入门之异步调用

    dubbo默认使用同步的方式调用.但在有些特殊的场景下,我们可能希望异步调用dubbo接口,从而避免不必要的等待时间,这时候我们就需要用到异步.那么dubbo的异步是如何实现的呢?下面就来看看这个问题 ...

  7. 使用vue-cli脚手架和vue-router搭建项目(一)

    之前做的项目一直比较简单,并没有引入整个路由库.今天准备练习下

  8. leetcood学习笔记-437-路径总和③**

    题目描述: 方法一:栈 class Solution(object): def pathSum(self, root, sum): """ :type root: Tre ...

  9. Spring-Security (学习记录五)--配置登录时,密码采用md5加密,以及获取登录信息属性监听同步自己想要的登录信息

    目录 1. PasswordEncoder 采用密码加密 2. 获取当前的用户信息 1. PasswordEncoder 采用密码加密 使用前面的例子.可以看出我们数据库密码是采用明文的,我们在登录的 ...

  10. python爬取文件时,内容为空

    解决方式: img_res = requests.get(src,headers=header)在header中加上referer防盗链加上防盗链header的例子: header = {" ...