转: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. linux常用命令-3文件与目录相关命令

    cd .. #返回上一级目录 cd ../.. #返回上两级目录 cd - #返回上次所在目录 cp file1 file2 #将file1复制为file2 cp -a dir1 dir2 #复制一个 ...

  2. 【学术篇】SDOI2009 SuperGCD

    特别说明: 为了避免以后搬家时的麻烦, 这里的文章继续沿用csdn的风格和分类好了~ Emmmm这个题是一道高精度的模板题啊~ 既然是高精度的裸题, 那我们这些懒人当然是选择:用python啦~ 懒癌 ...

  3. map 结构体

    map<node,int> 需要运算符重载< 请注意,不同的node,请务必让它们可以区分出来(node a,b a<b or b<a) 如 node { int a,i ...

  4. c++ 在类函数后加const的意义

    我们定义的类的成员函数中,常常有一些成员函数不改变类的数据成员,也就是说,这些函数是"只读"函数,而有一些函数要修改类数据成员的值.如果把不改变数据成员的函数都加上const关键字 ...

  5. DELPHI 让子窗体显示在任务栏上

    重载 CreateParams 方法即可 声明: procedure CreateParams(var Params: TCreateParams);override; procedure TForm ...

  6. mysql函数操作-增加自定义函数

    #首先执行看mysql是否开启函数SHOW VARIABLES LIKE '%func%';------------------------------------------------------ ...

  7. 2018-2019-2-20175323 java实验四 Android程序设计

    (一)安装及配置Andriod Studio,执行HelloWorld 我选择的安装网址下载了3.2.0版本的Andriod Studio 此处应该选择cancel 报错 点击所给链接,安装相应SDK ...

  8. 『BASH』——Learn BashScript from Daniel Robbins——[001-002]

    ABSTRACT: Daniel Robbins is best known as the creator of Gentoo Linux and author of many IBM develop ...

  9. Android 7.0 IMS框架详解

    本文主要讲解IP Multimedia Subsystem (IMS)在Android 7.0上由谷歌Android实现的部分内容.从APP侧一直到Telephony Framework,是不区分CS ...

  10. sklearn参数优化

    学习器模型中一般有两个参数:一类参数可以从数据中学习估计得到,还有一类参数无法从数据中估计,只能靠人的经验进行指定,后一类参数就叫超参数 比如,支持向量机里的C,Kernel,gama,朴素贝叶斯里的 ...