Android-自定义标题栏
2014年4月25日 分享知识点
近期也比较多事情,想发发博客就是心有余而力不足
,本篇博文主要教大家如何实现自定义标题栏,很简单,那么聪明的你一下就看懂。
实现步骤
* 1、给自定义标题提供一个界面
* 2、将自定义标题应用给Activity窗口
* 3、把android系统为Activity设置的默认主题改为自己的主题
效果图:
代码下载:
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:background="@drawable/rectangle"
6. android:orientation="horizontal" >
7.
8. <!-- 指定背景,该背景自己画的 -->
9.
10. <TextView
11. style="@android:style/TextAppearance.Medium"
12. android:layout_width="wrap_content"
13. android:layout_height="wrap_content"
14. android:layout_gravity="center_vertical"
15. android:text="IT_xiao小巫"
16. android:textColor="#ffffff"
17. android:textSize="14sp" />
18.
19. </LinearLayout>
这里使用到了一个图像资源,是在drawable目录下的:
/02_CustomTitle/res/drawable/rectangle.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
3. android:shape="rectangle" >
4.
5. <!-- 定义渐变色 -->
6. <gradient
7. android:angle="270"
8. android:endColor="#80FF00FF"
9. android:startColor="#FFFF0000" />
10. <!-- 定义控件内容到边界的距离(到四条边界的距离都是2) -->
11. <padding
12. android:bottom="2dp"
13. android:left="2dp"
14. android:right="2dp"
15. android:top="2dp" />
16. <!-- 定义圆角 -->
17. <corners android:radius="8dp" />
18.
19. </shape>
/02_CustomTitle/src/com/wwj/constomtitle/MainActivity.java
1. package com.wwj.constomtitle;
2.
3. import android.app.Activity;
4. import android.os.Bundle;
5. import android.view.Window;
6.
7. /**
8. * 1、给自定义标题提供一个界面
9. * 2、将自定义标题应用给Activity窗口
10. * 3、把android系统为Activity设置的默认主题改为自己的主题
11. *
12. * @author wwj
13. *
14. */
15. public class MainActivity extends Activity {
16.
17. @Override
18. protected void onCreate(Bundle savedInstanceState) {
19. super.onCreate(savedInstanceState);
20.
21. // 指定使用自定义标题
22. requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
23. setContentView(R.layout.activity_main);
24. // 设置窗口的自定义标题布局文件
25. getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.constom_title);
26.
27. }
28.
29.
30. }
修改默认样式
1. <!-- 该样式继承系统的默认样式 -->
2. <style name="customTheme" parent="android:Theme">
3.
4. <!-- 设置标题前景色为透明 -->
5. <item name="android:windowContentOverlay">@drawable/nocolor</item>
6. <!-- 设置标题高度为44dp -->
7. <item name="android:windowTitleSize">44dp</item>
8. <!-- 设置标题背景色 -->
9. <item name="android:windowTitleBackgroundStyle">@style/customBg</item>
10. </style>
11. <!-- 定义一个背景样式 -->
12. <style name="customBg">
13. <item name="android:background">@drawable/rectangle</item>
14. </style>
/02_CustomTitle/res/values/drawable.xml
1. <pre code_snippet_id="311163" snippet_file_name="blog_20140425_5_585886" name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
2. <resources>
3.
4. <!-- 定义一个透明色 -->
5. <drawable name="nocolor">#00000000</drawable>
6.
7. </resources></pre><br><br>
1. 在AndroidManifest.xml设置主题
1. <pre code_snippet_id="311163" snippet_file_name="blog_20140425_7_8154861" name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3. package="com.wwj.constomtitle"
4. android:versionCode="1"
5. android:versionName="1.0" >
6.
7. <uses-sdk
8. android:minSdkVersion="8"
9. android:targetSdkVersion="18" />
10.
11. <application
12. android:allowBackup="true"
13. android:icon="@drawable/ic_launcher"
14. android:label="@string/app_name"
15. android:theme="@style/customTheme" >
16. <activity
17. android:name="com.wwj.constomtitle.MainActivity"
18. android:label="@string/app_name" >
19. <intent-filter>
20. <action android:name="android.intent.action.MAIN" />
21.
22. <category android:name="android.intent.category.LAUNCHER" />
23. </intent-filter>
24. </activity>
25. </application>
26.
27. </manifest></pre><br><br>