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]
發佈了40 篇原創文章 · 獲贊 2 · 訪問量 3308
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章