转: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. Intellij IDEA gradle项目目录介绍

    Gradle简介 Java的构建,经历了从Ant-->Maven->Gradle的过程,每一次的进步,都是为了解决之前的工具带来的问题: Ant:Ant的功能虽然强大,但过于灵活,规范性不 ...

  2. IDEA使用maven插件打jar包流程

    idea使用maven插件打jar包步骤以及遇到的问题 idea自带了maven工具,idea右边点击maven选项: 一.在pom中添加插件,直接复制就好,如下选项 <plugin> & ...

  3. CF838C(博弈+FWT子集卷积+多项式ln、exp)

    传送门: http://codeforces.com/problemset/problem/838/C 题解: 如果一个字符串的排列数是偶数,则先手必胜,因为如果下一层有后手必赢态,直接转移过去,不然 ...

  4. Electron是个啥?

    于2013年作为构建Github上可编辑的文本编辑器Atom的框架而被开发出来 是目前开源开发者.初创企业和老牌公司常用的开发工具 是桌面应用框架 相当于一个浏览器的外壳,可以把网页程序嵌入到壳里面, ...

  5. 移动端图片轮播效果:depth模式总结

    最近公司app改版首页增加了一处轮播图效果,但是跟普通的轮播效果不同,是类似于下图的样式,找了一些兼容移动端的插件以及jQuery源码,总结一下使用心得: 1:jquery源码:缺点是在手机端的滑动很 ...

  6. NX二次开发-UFUN适应窗口UF_VIEW_fit_view

    NX9+VS2012 #include <uf.h> #include <uf_view.h> #include <uf_modl.h> UF_initialize ...

  7. POJ3241 最小曼哈顿距离生成树 - 真有趣哇

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门  原题目描述在最下面.  给你n个坐标, ...

  8. R语言中动态安装库

    R语言中动态安装库 在一个R脚本中,我们使用了某些library,但是发现运行环境中没有这个library,如果能检测一下有没有这个包,没有就自动安装该多好.而R中非常方便地支持这些,只要联网. 代码 ...

  9. (转)sql的group by应用

    转载于:http://www.studyofnet.com/news/247.html 本文导读:在实际SQL应用中,经常需要进行分组聚合,即将查询对象按一定条件分组,然后对每一个组进行聚合分析.创建 ...

  10. 修改linux默认语言

    linux修改默认语言   编辑/etc/sysconfig/i18n这个文件(不存在就新建一个),原内容如下: LANG="en_US.UTF-8" SYSFONT=" ...