Android-自己定義標題欄
Android-自己定義標題欄
2014年4月25日 分享知識點
最近也比較多事情,想發發博客就是心有余而力不足,本篇博文主要教大家怎樣實現自己定義標題欄,非常easy。那麽聰明的你一下就看懂。
有興趣能夠加一下 群號是299402133,裏面有豐富的學習資源,誌同道合的你。一定會有所收獲的。
實現步驟
* 1、給自己定義標題提供一個界面
* 2、將自己定義標題應用給Activity窗體
* 3、把android系統為Activity設置的默認主題改為自己的主題
效果圖:
代碼下載:http://download.csdn.net/detail/wwj_748/7249585
/02_CustomTitle/res/layout/constom_title.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/rectangle" android:orientation="horizontal" > <!-- 指定背景。該背景自己畫的 --> <TextView style="@android:style/TextAppearance.Medium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="IT_xiao小巫" android:textColor="#ffffff" android:textSize="14sp" /> </LinearLayout>
這裏使用到了一個圖像資源,是在drawable文件夾下的:
/02_CustomTitle/res/drawable/rectangle.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!-- 定義漸變色 --> <gradient android:angle="270" android:endColor="#80FF00FF" android:startColor="#FFFF0000" /> <!-- 定義控件內容到邊界的距離(到四條邊界的距離都是2) --> <padding android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp" /> <!-- 定義圓角 --> <corners android:radius="8dp" /> </shape>
package com.wwj.constomtitle; import android.app.Activity; import android.os.Bundle; import android.view.Window; /** * 1、給自己定義標題提供一個界面 * 2、將自己定義標題應用給Activity窗體 * 3、把android系統為Activity設置的默認主題改為自己的主題 * * @author wwj * */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 指定使用自己定義標題 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_main); // 設置窗體的自己定義標題布局文件 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.constom_title); } }
改動默認樣式
<!-- 該樣式繼承系統的默認樣式 --> <style name="customTheme" parent="android:Theme"> <!-- 設置標題前景色為透明 --> <item name="android:windowContentOverlay">@drawable/nocolor</item> <!-- 設置標題高度為44dp --> <item name="android:windowTitleSize">44dp</item> <!-- 設置標題背景色 --> <item name="android:windowTitleBackgroundStyle">@style/customBg</item> </style> <!-- 定義一個背景樣式 --> <style name="customBg"> <item name="android:background">@drawable/rectangle</item> </style>
/02_CustomTitle/res/values/drawable.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- 定義一個透明色 --> <drawable name="nocolor">#00000000</drawable> </resources>
在AndroidManifest.xml設置主題
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wwj.constomtitle" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/customTheme" > <activity android:name="com.wwj.constomtitle.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Android-自己定義標題欄