android中如何自定義標題欄
阿新 • • 發佈:2019-01-08
首先,修改標題欄的寬度和背景,在strings.xml中新增:
<item name="android:background">@drawable/title_bg</item> </style> <style name="test" parent="android:Theme"> <item name="android:windowTitleSize">40dp</item> <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> </style>
然後修改AndroidMainfest.xml檔案:
<activity android:name=".MainActivity" android:theme="@style/test"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
接著修改MainActivity:
最後在layout資料夾中新增title.xml檔案,內容如下:public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //宣告使用自定義標題 setContentView(R.layout.main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);//自定義佈局賦值 }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="wrap_content"
android:layout_centerVertical=’true’
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_email" />
<TextView android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:text="自定義標題欄" />
</RelativeLayout>
結果自由實現文字及 文字 圖片