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

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

Java代码     收藏代码
  1. requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
  2. setContentView(view);  
  3. 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 >   

         好,运行看效果。看到了吧,发现问题了没,标题栏的背景色没有填充满是吧,这可真是杯具哟。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 >   

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

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

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

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

Xml代码     收藏代码
  1. < 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 >   

      然后写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 >   

      注意这个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 >   

 

 

    


requestWindowFeature 是用来设置全屏的。

       在Activity中

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        getWindow.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

                 WindowManager.LayoutParams.FLAG_FULLSCREEN);

       在J2ME中仅仅需要setFullScreenMode(true);

如下实例:

   //全屏
   public void setFullscreen() {
           requestWindowFeature(Window.FEATURE_NO_TITLE);
           getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                   WindowManager.LayoutParams.FLAG_FULLSCREEN);
       }
      
   //无标题
       public void setNoTitle() {
           requestWindowFeature(Window.FEATURE_NO_TITLE);
       }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值