学习Android自定义标题栏时,会发现即使layout_width设置为fill_parent或者match_parent之后,在标题栏的左右会留有一部分空间,如下图
这是系统默认的样式,解决方法:
自定义样式,继承自系统的主题,设置android:windowTitleBackgroundStyle属性为@null,然后再主配置文件中把Activity的主题设置为自定的MyTheme即可。
第一种:在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>
在主配置文件中把Activity的主题设置为自定的MyTheme,
<activity android:name=".MenuDetailActivity"
android:theme="@style/MyTheme" >
第二种:在styles.xml中添加<item name="android:windowTitleBackgroundStyle">@null</item>,如下所示,
<style name="mystyle" parent="android:Theme">
<item name="android:windowTitleSize">50dp</item>
<item name="android:textColor">#000000</item>
<item name="android:textSize">18sp</item>
<item name="android:windowTitleBackgroundStyle">@null</item>
</style>
在主配置文件中把Activity的主题设置为自定的MyTheme,
<activity android:name=".MenuDetailActivity"
android:theme="@style/mystyle">
效果图: