`
LoveZhou
  • 浏览: 270378 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android 自定义标题栏 填满问题

阅读更多
Android 每个Activity界面,都会自动生成一个灰色的标题栏,在编写程序时,可以选择是否有标题栏,或者自定义标题栏,自定义标题栏时,可以在标题栏位置,放置一个布局
系统自带:


可以通过:
  this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
        this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);

将标题栏设置成自定义的布局文件:R.layout.custom_title
<?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:gravity="center_vertical"
    android:background="@drawable/category_bar"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="这是自定义标题栏"
    android:textColor="@android:color/black"
    />
</LinearLayout>

但发现带背景图的自定义标题栏布局无法充满屏幕(横向)


原因是系统默认的样式,预留了左右一小部分空间
解决办法是:
在values资源文件夹下建立my_style.xml文件,内容为:
<?xml version="1.0" encoding="utf-8"?>
<resources>  
    <style name="MyTheme" parent="android:Theme">       
        <item name="android:windowTitleBackgroundStyle">@null</item>        
    </style>     
</resources> 

自定义样式,继承自系统的主题,设置android:windowTitleBackgroundStyle属性为@null,然后再主配置文件中把Activity的主题设置为自定的MyTheme即可。
<activity android:name=".CustomTitleActivity" android:theme="@style/MyTheme"                 android:label="@string/app_name">
效果图:

  • 大小: 9.2 KB
  • 大小: 9.5 KB
  • 大小: 9.4 KB
分享到:
评论
1 楼 shangfyok 2011-07-13  

问题解决了

相关推荐

Global site tag (gtag.js) - Google Analytics