转: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. python操作redis数据

    一.环境安装 1.redispy安装 (automatic) C:\Users\Administrator>pip install redis 2.检测是否安装成功 (automatic) C: ...

  2. 小程序怎样将字符串转化为html

    在小程序中通过rich-text 标签,使用nodes将字符串转化为html <rich-text class="allAnswer "  nodes='<span c ...

  3. layui中load具体用法

    遮盖窗体,demo: layer.load(,{ // content: "加载中...", shade: [0.4,'#000'], //0.1透明度的白色背景 time:* } ...

  4. 【luoguP3701】「伪模板」主席树

    description byx和诗乃酱都非常都非常喜欢种树.有一天,他们得到了两颗奇怪的树种,于是各自取了一颗回家种树,并约定几年后比一比谁种出来的树更加牛x. 很快,这棵树就开花结果了.byx和诗乃 ...

  5. JAVA java调用C++动态链接库dll,有详细过程。VS2015+Eclipse以及失败解决方案

    一.新建Java工程,在Java类中声明一个native的方法 新建Java项目 在新建的项目中创建packet(包),并且在包下创建一个Class(类). 接下来,在该类中添加如下代码: ? 1 2 ...

  6. 思维题+贪心——牛客多校第一场C

    /* 给定一组n维向量 A=(a1/m,a2/m,a3/m ... an/m), 求另一个n维向量 P=(p1,p2,p3...pn),满足sum{pi}=1,使得ans=sum{(ai/m-pi)^ ...

  7. BZOJ 1697: [Usaco2007 Feb]Cow Sorting牛排序(置换+贪心)

    题面 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大的牛有可能会捣乱,JOHN想把牛按脾气的大小排序.每一头牛的脾气都 ...

  8. inobounce.js : 禁止IOS H5的滑动回弹

    IOS的移动端/H5/webapp 页面如果滚动到底部或者在页面顶部再往上拉,都会出现一个回弹的效果. 想取消这个效果可以引入一个简单的库就行,不用再写繁琐的样式. github地址 直接引入就行 & ...

  9. 数学相关比较 牛顿迭代法求开方 很多个n的平方分之一

    牛顿迭代法求开方 牛顿迭代法 作用: 求f(x) = 0 的解 方法:假设任意一点 x0, 求切线与x轴交点坐标x1, 再求切线与x轴交点坐标x2,一直重复,直到f(xn) 与0的差距在一个极小的范围 ...

  10. 《DSP using MATLAB》Problem 9.2

    前几天看了看博客,从16年底到现在,3年了,终于看书到第9章了.都怪自己愚钝不堪,唯有吃苦努力,一点一点一页一页慢慢啃了. 代码: %% ------------------------------- ...