Android 自定义标题栏 填满问题

Android 每个Activity界面,都会自动生成一个灰色的标题栏,在编写程序时,可以选择是否有标题栏,或者自定义标题栏,自定义标题栏时,可以在标题栏位置,放置一个布局
系统自带:
[img]http://dl.iteye.com/upload/attachment/384362/f373a3e9-11b9-365c-985d-02ce5f0feadb.png[/img]

可以通过:
  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>

但发现带背景图的自定义标题栏布局无法充满屏幕(横向)
[img]http://dl.iteye.com/upload/attachment/384364/18eed205-6b55-3889-ab3b-cc5236ea725c.png[/img]

原因是系统默认的样式,预留了左右一小部分空间
解决办法是:
在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" [color=red]android:theme="@style/MyTheme" [/color] android:label="@string/app_name">
效果图:
[img]http://dl.iteye.com/upload/attachment/384366/e6a501ad-da67-316e-8bb7-0b66e006bed4.png[/img]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值