Android 每个Activity界面,都会自动生成一个灰色的标题栏,在编写程序时,可以选择是否有标题栏,或者自定义标题栏,自定义标题栏时,可以在标题栏位置,放置一个布局
系统自带:
[img]http://dl.iteye.com/upload/attachment/384362/f373a3e9-11b9-365c-985d-02ce5f0feadb.png[/img]
可以通过:
将标题栏设置成自定义的布局文件:R.layout.custom_title
但发现带背景图的自定义标题栏布局无法充满屏幕(横向)
[img]http://dl.iteye.com/upload/attachment/384364/18eed205-6b55-3889-ab3b-cc5236ea725c.png[/img]
原因是系统默认的样式,预留了左右一小部分空间
解决办法是:
在values资源文件夹下建立my_style.xml文件,内容为:
自定义样式,继承自系统的主题,设置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]
系统自带:
[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]