`
java-admin
  • 浏览: 1389017 次
  • 性别: Icon_minigender_1
  • 来自: 陕西.西安
社区版块
存档分类
最新评论

解决android自定义标题栏充满的问题

阅读更多

一个接着一个的activity,写啊写,调啊调,后来,终于发觉,activity的标题栏好难看,好单调啊。咱们为了吸引用户的眼球,得搞点个性化的东西。
        自定义标题栏的方法,网上一搜一大堆,我也稍微提一下,oncreate中加上如下代码就行:

Java代码
  1. requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
  2. setContentView(view);  
  3. getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);  
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(view);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

         这个名为title的layout是这样子的,很简单,就是一个textview,然后有个背景色:

Xml代码
  1. <? xml   version = "1.0"   encoding = "utf-8" ?>   
  2. < LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"   
  3.     android:orientation = "vertical"   
  4.     android:layout_width = "fill_parent"   
  5.     android:layout_height = "fill_parent"   
  6.     android:background = "#66cccccc"   
  7.     >   
  8. < TextView     
  9.     android:layout_width = "fill_parent"    
  10.     android:layout_height = "wrap_content"    
  11.     android:text = "hello"   
  12.     />   
  13. </ LinearLayout >   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#66cccccc"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="hello"
    />
</LinearLayout>

         好,运行看效果。看到了吧,发现问题了没,标题栏的背景色没有填充满是吧,这可真是杯具哟。padding、margin什么的都用上也不管用,怎么办呢。
        看源码!
        window初始化,加载标题的地方,咱也不知道在哪里,不过咱能以layout作为切入点。打开源码里面的layout文件夹,找跟标题栏相关的xml 文件。里面有screen_title.xml和screen_custom_title.xml,这就是咱们要找的目标了。
        既然是自定义标题,那我们就看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"
    好,从这里我们就可以入手改了。

      去values下面的themes.xml找到windowTitleBackgroundStyle这一项,这个应该在注释<!-- Window attributes -->的下面。

Xml代码
  1. < item   name = "windowTitleBackgroundStyle" > @android:style/WindowTitleBackground </ item >   
<item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>

      然后去styles.xml下找到WindowTitleBackground项,

Xml代码
  1. < style   name = "WindowTitleBackground" >   
  2.         < item   name = "android:background" > @android:drawable/title_bar </ item >   
  3. </ style >   
<style name="WindowTitleBackground">
        <item name="android:background">@android:drawable/title_bar</item>
</style>

      发现是一个drawable,xml的,里面定义了背景图片。ok,我们知道了,这个是定义titlebar的背景色。

    然后,去values下面的themes.xml找到windowContentOverlay,也是属于window attributes。

Xml代码
  1. < item   name = "windowContentOverlay" > @android:drawable/title_bar_shadow </ item >   
<item name="windowContentOverlay">@android:drawable/title_bar_shadow</item>

     发现也是个drawable,ok,我们也知道了,这个是定义contentoverlay的背景的。
    其实,通过研究我发现,不能填充满的原因是title_container的背景的原因,我们覆盖一下就行了。

      首先,写个themes文件

 

Xml代码
  1. < resources >   
  2.     < style   name = "XTheme"   parent = "android:Theme" >       
  3.           
  4.         <!-- Window attributes -->   
  5.         < item   name = "android:windowTitleStyle" > @style/XWindowTitle </ item >      
  6.         < item   name = "android:windowTitleBackgroundStyle" > @style/StatusBarBackground </ item >        
  7.         < item   name = "android:windowContentOverlay" > @null </ item >   
  8.     </ style >     
  9. </ resources >   
<resources>
    <style name="XTheme" parent="android:Theme">    
    	
        <!-- Window attributes -->
        <item name="android:windowTitleStyle">@style/XWindowTitle</item>   
        <item name="android:windowTitleBackgroundStyle">@style/StatusBarBackground</item>     
        <item name="android:windowContentOverlay">@null</item>
	</style>	
</resources>

     然后写styles文件

Xml代码
  1. < resources >       
  2.     < style   name = "StatusBarBackground" >   
  3.         < item   name = "android:background" > @drawable/shape </ item >   
  4.     </ style >   
  5.               
  6.     < style   name = "XWindowTitle"   parent = "android:WindowTitle" >   
  7.         < item   name = "android:shadowColor" > #BB000000 </ item >   
  8.         < item   name = "android:shadowRadius" > 0 </ item >   
  9.     </ style >   
  10. </ resources >   
<resources>    
    <style name="StatusBarBackground">
        <item name="android:background">@drawable/shape</item>
    </style>
            
    <style name="XWindowTitle" parent="android:WindowTitle">
        <item name="android:shadowColor">#BB000000</item>
        <item name="android:shadowRadius">0</item>
    </style>
</resources>

     注意这个XWindowTitle要继承WindowTitle。

   最后,在manifext中给自定义的activity申明主题。

Xml代码
  1. < activity   android:name = ".Entry"   
  2.            android:label = "@string/app_name"   
  3.            android:theme = "@style/XTheme" >   
  4.      < intent-filter >   
  5.          < action   android:name = "android.intent.action.MAIN"   />   
  6.          < category   android:name = "android.intent.category.LAUNCHER"   />   
  7.      </ intent-filter >   
  8.  </ activity >   
       <activity android:name=".Entry"
                  android:label="@string/app_name"
                  android:theme="@style/XTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

     好,我们来看看效果吧:

      so cool, isn't it?

      当然,你也可以换成别的颜色或者是更炫的图片做背景。

        详细实例代码见附件。

 

http://chroya.iteye.com/blog/760314

 

 

分享到:
评论
1 楼 wenjundiandian 2012-01-06  
为什么 我的R.layout.title会报错,另外,WindowTitle这个地方也会报错     

相关推荐

    customTitleBar

    在Android或iOS等移动应用开发中,自定义标题栏(customTitleBar)是常见的需求,它可以帮助开发者根据自己的设计风格和功能需求定制应用程序的界面。本文将深入探讨如何实现自定义标题栏,包括如何调整其高度并使其...

    新版Android开发教程.rar

    ----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...

    蓝牙开发详解

    这里,`custom_title`是自定义标题栏的布局文件,通常包含应用程序的标题和其他控件,如返回按钮或菜单选项。 #### 总结 蓝牙开发在Android平台上是一个复杂但充满潜力的过程,它允许开发者创建能够连接和控制外部...

    一个沉浸式设计Demo

    在Android开发领域,沉浸式设计(也称为全屏体验或无边界设计)是一种追求极致用户体验的设计模式,它能够提供更为广阔、无干扰的视觉效果,让应用内容充满整个屏幕,从而提升用户的沉浸感。"一个沉浸式设计Demo...

    android音乐播放器源码

    在Android平台上开发一款音乐播放器是一项技术性强且充满挑战的任务。这个"android4.0音乐播放器源码"提供了一个很好的学习平台,帮助开发者深入理解Android应用开发,特别是与音频处理和用户界面设计相关的技术。...

    10个插件让你的iphone比android更好玩(精编版).pdf

    【标题】:“10个插件让你的iPhone比Android更好玩(精编版)” 【描述】:本文介绍了一系列能够提升iPhone用户体验的越狱插件,使得iPhone在功能和个性化方面超越Android设备。 【标签】:iPhone, 越狱, 插件, ...

    Android5.0新控件实例详解

    Toolbar 支持比 ActionBar 更集中的特征,例如导航按钮、品牌的 Logo 图像、标题和子标题、一个或多个自定义视图等。 ```java this.toolbar = (Toolbar) findViewById(R.id.toolbar); this.recyclerview = ...

    toolbarsearchview

    在实现标题居中这一需求时,我们需要在布局文件中设置`Toolbar`的`android:title`属性或者在代码中调用`setTitle()`方法来设置标题,然后通过调整`Toolbar`的样式或使用自定义的`TextView`实现标题居中对齐。...

    Android 九宫格的实现方法

    在`MainActivity`的`onCreate()`方法中,首先去掉了标题栏,然后设置了布局。关键步骤是获取到`GridView`实例并填充数据。这里使用了`ArrayList, Object&gt;&gt;`来存储每个单元格的数据,每个`HashMap`代表一个单元格的...

    java安卓源码特效-9GAG:9GAG-Android(非官方),Android设计

    java安卓源码特效 9GAG-Android (unofficial) 9GAG 安卓客户端个人版 9GAG安卓客户端,遵从Android Design 作者大三学生一枚,详见about页面,有意...7.ActionBar标题字体自定义 8.Toast自定义 9.支持分享图片+文本,经

    移动端开发常见问题总结

    为了提供更好的用户体验,当用户将应用添加到主屏幕后,可以自定义标题。这可以通过以下meta标签实现: ```html 标题"&gt; ``` 这里的`content`属性就是显示在主屏幕上的应用名称。这对于提升品牌识别度和用户体验很...

Global site tag (gtag.js) - Google Analytics