Android 自定义 标题
2012-02-08 10:16
176 查看
一个接着一个的activity,写啊写,调啊调,后来,终于发觉,activity的标题栏好难看,好单调啊。咱们为了吸引用户的眼球,得搞点个性化的东西。
自定义标题栏的方法,网上一搜一大堆,我也稍微提一下,oncreate中加上如下 代码 就行:
Java代码 6
J/ ]6 F" x0 R( ?: R, w
1.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
2.setContentView(view); $
d! i) H. B/ Y2 h1 k9 L
3.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); +
d0 t+ v0 C( ?+ u
setContentView(view);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title); 这个名为title的layout是这样子的,很简单,就是一个textview,然后有个背景色:
2
Y- ], m0 w1 h5 [, l
Xml代码 /
y/ ?$ A* r& \* W
1.<?xml version="1.0" encoding="utf-8"?>
2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" !
K( Y q% I7 h" x
3. android:orientation="vertical" 5
F4 B" l1 n- s
4. android:layout_width="fill_parent" 1
v( v" w! R1 Y* m- G- A
5. android:layout_height="fill_parent" 5
y, D7 f1 Z: \2 L! ] \
6. android:background="#66cccccc" 2
F, B' g$ y) q; I+ U2 k
7. >
8.<TextView (
q; r- d4 }, W. }& M7 `
9. android:layout_width="fill_parent" x3
x; c/ R: W7 Z; T- J
10. android:layout_height="wrap_content" 1
^% B! W$ l& r( s( N7 I% x
11. android:text="hello"
12. />
13.</LinearLayout> ,
I7 R8 N6 S g7 |
<?xml version="1.0" encoding="utf-8"?> ;
A) e3 O5 C! Y1 P9 S" A, V8 x
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" ;
t2 g3 P0 V, R8 S9 K
android:layout_width="fill_parent" "
_. U0 L+ p: r' W4 R1 s8 f
android:layout_height="fill_parent"
android:background="#66cccccc" %
T1 V; Q2 ]3 A3 }* ^. a
> 0
A; b* H* m0 e- V9 T
<TextView
android:layout_width="fill_parent" 1
m; B3 ^: P$ Q/ \+ {. L
android:layout_height="wrap_content"
android:text="hello" (
}6 l+ E8 i/ P$ e, V( i k5 q
/>
</LinearLayout> 好,运行看效果。看到了吧,发现问题了没,标题栏的背景色没有填充满是吧,这可真是杯具哟。padding、margin什么的都用上也不管用,怎么办呢。 6
^" [- x2 Q! G' u0 O
看 源码 ! )
b' c. {2 v( _+ m: n
window初始化,加载标题的地方,咱也不知道在哪里,不过咱能以layout作为切入点。打 开源 码里面的layout 文件 夹,找跟标题栏相关的xml文件。里面有screen_title.xml和screen_custom_title.xml,这就是咱们要找的目标了。 2
h4 w6 S9 d# W& F5 T
既然是自定义标题,那我们就看screen_custom_title.xml,里面有一个title_container和一个content,组合成了标题栏,我们自定义标题所给出的view,都被content作为子view了,影响不了那个title_container和content,所以,任你怎么弄,它该留白的还是留白,你没招。
看title_container有个style是这样的:style="?android:attr/windowTitleBackgroundStyle"
content的foreground是这样的android:foreground="?android:attr/windowContentOverlay" )
J! J( u. f4 p4 i4 F$ F' ~* C
好,从这里我们就可以入手改了。 .
P" c/ c2 ?/ L1 P/ h6 }: r1 i
去values下面的themes.xml找到windowTitleBackgroundStyle这一项,这个应该在注释<!-- Window attributes -->的下面。 +
B: Y2 B1 J3 K* ?
7
t) _) S y- b# {* r( G( g
Xml代码 8
H9 X+ a+ q; H0 j5 y
1.<item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
<item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item> 然后去styles.xml下找到WindowTitleBackground项,
$
P: m2 W- z6 h! a5 s1 K. }8 S8 i a
Xml代码
1.<style name="WindowTitleBackground">
2. <item name="android:background">@android:drawable/title_bar</item>
3.</style> !
L& G9 ?( t' Y, j! l* r+ U4 ^
<style name="WindowTitleBackground"> )
}& R5 N7 q7 Y# c
<item name="android:background">@android:drawable/title_bar</item>
</style> 发现是一个drawable,xml的,里面定义了背景图片。ok,我们知道了,这个是定义titlebar的背景色。 !
c* I, }2 m$ H# a& _
!
C# A5 D9 q0 ^" Q6 d
然后,去values下面的themes.xml找到windowContentOverlay,也是属于window attributes。 "
N1 v4 ^; a9 U- R# _- F
Xml代码
1.<item name="windowContentOverlay">@android:drawable/title_bar_shadow</item> '
P: ?0 B$ B; x# r
<item name="windowContentOverlay">@android:drawable/title_bar_shadow</item> 发现也是个drawable,ok,我们也知道了,这个是定义contentoverlay的背景的。
其实,通过研究我发现,不能填充满的原因是title_container的背景的原因,我们覆盖一下就行了。 5
X/ _% h8 J* t- L4 ~; f( L
首先,写个themes文件 8
g |3 W9 z/ w( D
#
a) g; G, E5 @9 V7 d, Q# {
Xml代码
1.<resources>
2. <style name="XTheme" parent="android:Theme"> %
g. n1 b: o! ^. c- S+ k" P
3. 3
j3 K( ?/ g- @# ]
4. <!-- Window attributes -->
5. <item name="android:windowTitleStyle">@style/XWindowTitle</item> 4
[1 c% m: X( \; ]* a" z x& d
6. <item name="android:windowTitleBackgroundStyle">@style/StatusBarBackground</item>
7. <item name="android:windowContentOverlay">@null</item> -
q. q7 p: L6 j+ r( R5 C& I
8. </style> *
t0 `) u' \ l& V
9.</resources> /
l e+ C/ t8 d% q+ d
<resources> :
?; R( E6 g' z) O+ O% m
<style name="XTheme" parent="android:Theme">
<!-- Window attributes --> "
J1 S, ]+ [. G! {- I
<item name="android:windowTitleStyle">@style/XWindowTitle</item>
<item name="android:windowTitleBackgroundStyle">@style/StatusBarBackground</item> -
Y/ ?) u0 i, H6 l0 K. y2 \$ c
<item name="android:windowContentOverlay">@null</item> *
F# V' T" u: y& j7 e
</style> 6
j1 o" i* c; H/ _2 H3 S+ X1 g4 G& }
</resources> 然后写styles文件
Xml代码 :
h8 ~) s6 y+ V* o4 e; i
1.<resources>
2. <style name="StatusBarBackground"> *
V2 F! G/ o7 i/ g* p+ h0 } Y$ R
3. <item name="android:background">@drawable/shape</item>
4. </style>
5. #
Y# {' O: l8 d+ u8 w
6. <style name="XWindowTitle" parent="android:WindowTitle"> '
\ s8 h6 _7 K3 u4 z" L5 |' A
7. <item name="android:shadowColor">#BB000000</item> &
r" Y4 W' t2 T* U/ P' T
8. <item name="android:shadowRadius">0</item> ,
y5 m0 A" k. V1 z6 [) Q; V% ^1 t/ A
9. </style>
10.</resources>
<resources> "
}& a8 n& j, |! K h
<style name="StatusBarBackground"> 2
S8 K9 n! u o1 D# z |! [
<item name="android:background">@drawable/shape</item> 6
?8 o) o; t5 [
</style> 6
a+ }) z4 c" o. w
+
?7 u3 F" P1 P8 d4 j3 X
<style name="XWindowTitle" parent="android:WindowTitle"> 9
O9 R/ ~% I: n, Y; @+ F, ^2 o
<item name="android:shadowColor">#BB000000</item>
<item name="android:shadowRadius">0</item>
</style>
</resources> 注意这个XWindowTitle要继承WindowTitle。 4
N6 i& Z& B$ |
最后,在manifext中给自定义的activity申明主题。
Xml代码
1.<activity android:name=".Entry"
2. android:label="@string/app_name"
3. android:theme="@style/XTheme"> 1
y6 X' W3 ]2 k( p6 ~, T; t* }
4. <intent-filter>
5. <action android:name="android.intent.action.MAIN" /> 1
{ X' Y- B5 @* D& T3 G H- X+ {
6. <category android:name="android.intent.category.LAUNCHER" />
7. </intent-filter> 8
|- ~* S. T$ ]7 H( y
8. </activity>
效果图
so
cool, isn't it? *
^# v" a. n3 X1 V5 d# c o c
当然,你也可以换成别的颜色或者是更炫的图片做背景。
自定义标题栏的方法,网上一搜一大堆,我也稍微提一下,oncreate中加上如下 代码 就行:
Java代码 6
J/ ]6 F" x0 R( ?: R, w
1.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
2.setContentView(view); $
d! i) H. B/ Y2 h1 k9 L
3.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); +
d0 t+ v0 C( ?+ u
setContentView(view);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title); 这个名为title的layout是这样子的,很简单,就是一个textview,然后有个背景色:
2
Y- ], m0 w1 h5 [, l
Xml代码 /
y/ ?$ A* r& \* W
1.<?xml version="1.0" encoding="utf-8"?>
2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" !
K( Y q% I7 h" x
3. android:orientation="vertical" 5
F4 B" l1 n- s
4. android:layout_width="fill_parent" 1
v( v" w! R1 Y* m- G- A
5. android:layout_height="fill_parent" 5
y, D7 f1 Z: \2 L! ] \
6. android:background="#66cccccc" 2
F, B' g$ y) q; I+ U2 k
7. >
8.<TextView (
q; r- d4 }, W. }& M7 `
9. android:layout_width="fill_parent" x3
x; c/ R: W7 Z; T- J
10. android:layout_height="wrap_content" 1
^% B! W$ l& r( s( N7 I% x
11. android:text="hello"
12. />
13.</LinearLayout> ,
I7 R8 N6 S g7 |
<?xml version="1.0" encoding="utf-8"?> ;
A) e3 O5 C! Y1 P9 S" A, V8 x
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" ;
t2 g3 P0 V, R8 S9 K
android:layout_width="fill_parent" "
_. U0 L+ p: r' W4 R1 s8 f
android:layout_height="fill_parent"
android:background="#66cccccc" %
T1 V; Q2 ]3 A3 }* ^. a
> 0
A; b* H* m0 e- V9 T
<TextView
android:layout_width="fill_parent" 1
m; B3 ^: P$ Q/ \+ {. L
android:layout_height="wrap_content"
android:text="hello" (
}6 l+ E8 i/ P$ e, V( i k5 q
/>
</LinearLayout> 好,运行看效果。看到了吧,发现问题了没,标题栏的背景色没有填充满是吧,这可真是杯具哟。padding、margin什么的都用上也不管用,怎么办呢。 6
^" [- x2 Q! G' u0 O
看 源码 ! )
b' c. {2 v( _+ m: n
window初始化,加载标题的地方,咱也不知道在哪里,不过咱能以layout作为切入点。打 开源 码里面的layout 文件 夹,找跟标题栏相关的xml文件。里面有screen_title.xml和screen_custom_title.xml,这就是咱们要找的目标了。 2
h4 w6 S9 d# W& F5 T
既然是自定义标题,那我们就看screen_custom_title.xml,里面有一个title_container和一个content,组合成了标题栏,我们自定义标题所给出的view,都被content作为子view了,影响不了那个title_container和content,所以,任你怎么弄,它该留白的还是留白,你没招。
看title_container有个style是这样的:style="?android:attr/windowTitleBackgroundStyle"
content的foreground是这样的android:foreground="?android:attr/windowContentOverlay" )
J! J( u. f4 p4 i4 F$ F' ~* C
好,从这里我们就可以入手改了。 .
P" c/ c2 ?/ L1 P/ h6 }: r1 i
去values下面的themes.xml找到windowTitleBackgroundStyle这一项,这个应该在注释<!-- Window attributes -->的下面。 +
B: Y2 B1 J3 K* ?
7
t) _) S y- b# {* r( G( g
Xml代码 8
H9 X+ a+ q; H0 j5 y
1.<item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
<item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item> 然后去styles.xml下找到WindowTitleBackground项,
$
P: m2 W- z6 h! a5 s1 K. }8 S8 i a
Xml代码
1.<style name="WindowTitleBackground">
2. <item name="android:background">@android:drawable/title_bar</item>
3.</style> !
L& G9 ?( t' Y, j! l* r+ U4 ^
<style name="WindowTitleBackground"> )
}& R5 N7 q7 Y# c
<item name="android:background">@android:drawable/title_bar</item>
</style> 发现是一个drawable,xml的,里面定义了背景图片。ok,我们知道了,这个是定义titlebar的背景色。 !
c* I, }2 m$ H# a& _
!
C# A5 D9 q0 ^" Q6 d
然后,去values下面的themes.xml找到windowContentOverlay,也是属于window attributes。 "
N1 v4 ^; a9 U- R# _- F
Xml代码
1.<item name="windowContentOverlay">@android:drawable/title_bar_shadow</item> '
P: ?0 B$ B; x# r
<item name="windowContentOverlay">@android:drawable/title_bar_shadow</item> 发现也是个drawable,ok,我们也知道了,这个是定义contentoverlay的背景的。
其实,通过研究我发现,不能填充满的原因是title_container的背景的原因,我们覆盖一下就行了。 5
X/ _% h8 J* t- L4 ~; f( L
首先,写个themes文件 8
g |3 W9 z/ w( D
#
a) g; G, E5 @9 V7 d, Q# {
Xml代码
1.<resources>
2. <style name="XTheme" parent="android:Theme"> %
g. n1 b: o! ^. c- S+ k" P
3. 3
j3 K( ?/ g- @# ]
4. <!-- Window attributes -->
5. <item name="android:windowTitleStyle">@style/XWindowTitle</item> 4
[1 c% m: X( \; ]* a" z x& d
6. <item name="android:windowTitleBackgroundStyle">@style/StatusBarBackground</item>
7. <item name="android:windowContentOverlay">@null</item> -
q. q7 p: L6 j+ r( R5 C& I
8. </style> *
t0 `) u' \ l& V
9.</resources> /
l e+ C/ t8 d% q+ d
<resources> :
?; R( E6 g' z) O+ O% m
<style name="XTheme" parent="android:Theme">
<!-- Window attributes --> "
J1 S, ]+ [. G! {- I
<item name="android:windowTitleStyle">@style/XWindowTitle</item>
<item name="android:windowTitleBackgroundStyle">@style/StatusBarBackground</item> -
Y/ ?) u0 i, H6 l0 K. y2 \$ c
<item name="android:windowContentOverlay">@null</item> *
F# V' T" u: y& j7 e
</style> 6
j1 o" i* c; H/ _2 H3 S+ X1 g4 G& }
</resources> 然后写styles文件
Xml代码 :
h8 ~) s6 y+ V* o4 e; i
1.<resources>
2. <style name="StatusBarBackground"> *
V2 F! G/ o7 i/ g* p+ h0 } Y$ R
3. <item name="android:background">@drawable/shape</item>
4. </style>
5. #
Y# {' O: l8 d+ u8 w
6. <style name="XWindowTitle" parent="android:WindowTitle"> '
\ s8 h6 _7 K3 u4 z" L5 |' A
7. <item name="android:shadowColor">#BB000000</item> &
r" Y4 W' t2 T* U/ P' T
8. <item name="android:shadowRadius">0</item> ,
y5 m0 A" k. V1 z6 [) Q; V% ^1 t/ A
9. </style>
10.</resources>
<resources> "
}& a8 n& j, |! K h
<style name="StatusBarBackground"> 2
S8 K9 n! u o1 D# z |! [
<item name="android:background">@drawable/shape</item> 6
?8 o) o; t5 [
</style> 6
a+ }) z4 c" o. w
+
?7 u3 F" P1 P8 d4 j3 X
<style name="XWindowTitle" parent="android:WindowTitle"> 9
O9 R/ ~% I: n, Y; @+ F, ^2 o
<item name="android:shadowColor">#BB000000</item>
<item name="android:shadowRadius">0</item>
</style>
</resources> 注意这个XWindowTitle要继承WindowTitle。 4
N6 i& Z& B$ |
最后,在manifext中给自定义的activity申明主题。
Xml代码
1.<activity android:name=".Entry"
2. android:label="@string/app_name"
3. android:theme="@style/XTheme"> 1
y6 X' W3 ]2 k( p6 ~, T; t* }
4. <intent-filter>
5. <action android:name="android.intent.action.MAIN" /> 1
{ X' Y- B5 @* D& T3 G H- X+ {
6. <category android:name="android.intent.category.LAUNCHER" />
7. </intent-filter> 8
|- ~* S. T$ ]7 H( y
8. </activity>
效果图
so
cool, isn't it? *
^# v" a. n3 X1 V5 d# c o c
当然,你也可以换成别的颜色或者是更炫的图片做背景。
相关文章推荐
- Android自定义标题和按钮效果(附源码)
- Android:自定义窗口标题
- 我的Android进阶之旅------>Android自定义窗口标题实例
- Android 自定义标题
- Android dialog无标题栏样式style(for 自定义dialog)
- 分享:Android之自定义标题
- Android中自定义标题的titlebar
- Android自定义组合标题控件
- Android 通讯录(1)-----自定义ItemDecoration实现分类标题
- Android之自定义(上方标题随ViewPager手势慢慢滑动)
- Android 自带ToolBar自定义中间标题&分类
- Android使用setCustomTitle()方法自定义对话框标题
- Android Toolbar标题居中和自定义字体(toolbar是和drawerlayout联动情况下)
- Android自定义标题栏后,刚进入应用会出现黑色的标题背景栏的解决方案
- 方法选择Android从SD卡中选择图片的三种方法及自定义系统选择框标题的方法
- android使用自定义标题时提示“you cannot combine custom titles with other  ”
- android 修改窗体标题的字体式样和背景图(自定义标题栏)
- Android自定义控件之标题控件
- android自定义标题
- Android之自定义标题