职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 421|回复: 9

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

[复制链接]
曾经的小孩 发表于 2011-7-18 09:19 | 显示全部楼层 |阅读模式
         一个接着一个的activity,写啊写,调啊调,后来,终于发觉,activity的标题栏好难看,好单调啊。咱们为了吸引用户的眼球,得搞点个性化的东西。
        自定义标题栏的方法,网上一搜一大堆,我也稍微提一下,oncreate中加上如下代码就行:
  
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);   
setContentView(view);   
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代码  
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    androidrientation="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>  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    androidrientation="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代码  
<item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>  

<item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>      然后去styles.xml下找到WindowTitleBackground项,
Xml代码  
<style name="WindowTitleBackground">  
        <item name="android:background">@android:drawable/title_bar</item>  
</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代码  
<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代码  
<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>  

<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代码  
<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>  

<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代码  
<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>  

       <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?
      当然,你也可以换成别的颜色或者是更炫的图片做背景。
        详细实例代码见附件。
BeatifulTitleBar.zip (25.9 KB)
下载次数: 481

爱车车 发表于 2011-7-18 09:19 | 显示全部楼层
楼主这个方法不错。

gz-vps 发表于 2011-7-18 09:19 | 显示全部楼层
怎么看源码!

找不到我 发表于 2011-7-18 09:19 | 显示全部楼层
zhchzh1000
怎么看源码!

去http://android.git.kernel.org/下或者去网上搜别人下载好的源码包

ksdal 发表于 2011-7-18 09:19 | 显示全部楼层
原来自定义的左边多出来空白,是背景图造成的。
lz解释的非常好

找不到我 发表于 2011-7-18 09:19 | 显示全部楼层
在Eclipse里面怎么打开源码里面的layout下面的xml文件?

走就走吧 发表于 2011-7-18 09:19 | 显示全部楼层
sun201200204
在Eclipse里面怎么打开源码里面的layout下面的xml文件?

干嘛要用eclipse打开呢,随便用个编辑器editplus、vim什么的都可以,只是阅读,也不需要检查错误。

走失的猫咪 发表于 2011-7-18 09:20 | 显示全部楼层
这个方法好。赞

fl 发表于 2011-7-18 09:20 | 显示全部楼层
楼主的方法是很好,但是美中不足,怎样去改变他的宽度,怎样把"标题"弄到中间?

醉倚西风 发表于 2011-7-18 09:20 | 显示全部楼层
人家一般界面都是用ui来做的呢~~


您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-30 03:42 , Processed in 0.142100 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表