Android自定義title

這裏我在前面加了個logo,而且改變了title的背景和高度。

首先編寫title的佈局文件,title.xml:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”horizontal”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”@drawable/bg_title”
android:gravity=”center_vertical”>
<ImageView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:background=”@drawable/title_logo”
/>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/app_name”
android:textSize=”20sp”
android:layout_marginLeft=”80dip”
android:textColor=”#ffffff”
/>
</LinearLayout>

然後在Activity的onCreate()里加上這三句話:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title);

需要注意的是這三句話的順序不能變。到這裏只是改變了title的佈局,下面改變背景和高度,這就需要改變Activity的theme。

在values文件夾下新建文件style.xml:

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
<style name=”CustomWindowTitleBackground”>
<item name=”android:background”>@drawable/bg_title</item>
</style>
<style name=”title_style” parent=”android:Theme”>
<item name=”android:windowTitleSize”>44dip</item>
<item name=”android:windowTitleBackgroundStyle”>@style/CustomWindowTitleBackground</item>
</style>
</resources>

最後在AndroidManifest.xml裏面加上:

<activity android:name=”.sysinfo”
android:label=”@string/app_name”
android:screenOrientation=”portrait”
android:theme=”@style/title_style”
>

這樣就實現了自定義title。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章