自定義Android標題欄TitleBar佈局

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); //軟件activity的佈局 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); //titlebar爲本身標題欄的佈局

Layout下創建titlebar.xml: html

複製代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:orientation="horizontal" > <Button android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/back" android:textSize="18sp" android:textColor="#FF0000FF" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/share" android:textSize="18sp" android:textColor="#FFFFFF00" /> </LinearLayout>
複製代碼
這樣雖然能夠在必定程度上定製標題欄, 不過, 這裏沒法改變標題欄的高度和背景(背景設置以後會在兩端有兩個
很是難看的邊框). 聽說, 緣由是android 固有的.
這裏有修改方法:
  原理是這樣的. 直接像上述代碼那樣添加title僅僅是把一個子界面添加到原有的title上的, 並無改變原來的屬性,
好比 標題欄大小, 標題欄背景. 這些須要在theme 主題裏面定義.
  所以先定義一個style, 若修改背景請修改android:windowTitleBackgroundStyle
  若修改標題欄高度,請修改android:windowTitleSize
複製代碼
例子: <?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="CustomWindowTitleBackground"> <item name="android:background">#565656</item> </style> <style name="test" parent="android:Theme"> <item name="android:windowTitleSize">50dp</item> <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> </style> </resources>
複製代碼
在程序的android manifest.xml中對應:
  activity中添加屬性 android:theme = "@style/test" 就能夠了
複製代碼
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.guardian" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" > <activity android:name=".Guardian" android:label="@string/app_name" android:theme = "@style/test" //就在這裏 > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="4" /> </manifest>
複製代碼
以後藉助於設置自定義的標題欄xml文件,就能夠自定義標題欄佈局了

相關文章
相關標籤/搜索