2011年08月12日
RelativeLayout 的使用實例 類似自定義TabHost效果
TabHost如果要自定義顯示的效果,有點麻煩,而默認的樣式有時候又與我們程序的風格不匹配.今天我們就用RelativeLayout來 實現與TabHost相同的功能.上效果圖:
點擊上面的tab,tab自身樣式會改變,下面內容也會改變,功能完全與TabHost相同.
介紹一下RelativeLayout,RelativeLayout是相對布局,顧名思義,就是說里面的控件位置都是相對其他控件的位置而確定的.如上 面的效果,Tab相對于屏幕頂部對齊,底部按鈕相對于屏幕底部對齊.而內容則放在頂部的Tab和底部的按鈕中間.
所以所有被其他控件依賴定位的控件,必須先寫,也就是說,要實現上面的效果,XML中不是從上往下寫,而是先定上和下,再寫中間,因為中間的內容高度,位 置都依賴于它的上下控件.
實現TabHost效果的原理也簡單,點擊tab時設置被點擊和沒被點擊的的tab的背景,字體顏色即可顯示點擊效果.在點擊事件中,用View的 removeAllViews()方法清除中間控件的所有內容,再用addView方法添加需要的內容.
下面上代碼,布局XML:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<?xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:weightSum
=
"1"
android:background
=
"@color/white"
>
<LinearLayout
android:id
=
"@+id/topmenu"
android:orientation
=
"horizontal"
android:layout_alignParentTop
=
"true"
android:layout_width
=
"fill_parent"
android:layout_height
=
"40dip"
android:background
=
"@drawable/topback"
android:gravity
=
"bottom"
>
<!-- android:layout_alignParentTop 與父元素頂部是否對齊,這里true,就是把這個topmenu放屏幕頂部 -->
<LinearLayout
android:id
=
"@+id/task"
android:orientation
=
"horizontal"
android:layout_height
=
"35dip"
android:layout_width
=
"100dip"
android:background
=
"@drawable/textback"
>
<TextView
android:layout_width
=
"fill_parent"
android:id
=
"@+id/taskText"
android:layout_height
=
"fill_parent"
android:text
=
"計劃"
android:gravity
=
"center"
android:textSize
=
"20sp"
android:textColor
=
"@color/white"
/>
</LinearLayout
>
<LinearLayout
android:id
=
"@+id/accounts"
android:orientation
=
"horizontal"
android:layout_height
=
"35dip"
android:layout_width
=
"100dip"
>
<TextView
android:layout_width
=
"fill_parent"
android:id
=
"@+id/accountsText"
android:layout_height
=
"fill_parent"
android:text
=
"帳號"
android:gravity
=
"center"
android:textSize
=
"20sp"
android:textColor
=
"@color/green"
/>
</LinearLayout
>
<LinearLayout
android:id
=
"@+id/sended"
android:orientation
=
"horizontal"
android:layout_height
=
"35dip"
android:layout_width
=
"100dip"
>
<TextView
android:layout_width
=
"fill_parent"
android:id
=
"@+id/sendedText"
android:layout_height
=
"fill_parent"
android:text
=
"已發"
android:gravity
=
"center"
android:textSize
=
"20sp"
android:textColor
=
"@color/green"
/>
</LinearLayout
>
</LinearLayout
>
?
?
<Button
android:id
=
"@+id/button"
android:layout_alignParentBottom
=
"true"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"底部按鈕"
/>
<!-- layout_alignParentBottom 與父元素底部是否對齊,這里true,就是把這個Button放屏幕底部 -->
?
<!-- RelativeLayout必須先寫四周的控件,再寫中間的,這里先寫頂部的Tab和底部的Button,再寫中間的Content,因為中間的內容位置是不固定的 -->
<LinearLayout
android:id
=
"@+id/content"
android:orientation
=
"vertical"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:layout_below
=
"@id/topmenu"
android:layout_above
=
"@id/button"
>
</LinearLayout
>
<!-- layout_below,當前控件放在設定控件下方 .
android:layout_above 當前控件放在設定控件上方
這里配合使用,就是放在頂部tab和底部Button的中間
-->
?
</RelativeLayout
>
|
程序代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package
com.pocketdigi
;
?
import
android.app.Activity
;
import
android.os.Bundle
;
import
android.view.LayoutInflater
;
import
android.view.View
;
import
android.view.View.OnClickListener
;
import
android.widget.LinearLayout
;
import
android.widget.TextView
;
?
public
class
Main
extends
Activity
{
/** Called when the activity is first created. */
LinearLayout task, accounts, sended, content
;
TextView taskText, accountsText, sendedText
;
?
@Override
public
void
onCreate
(
Bundle savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
)
;
setContentView
(
R.
layout
.
main
)
;
task
=
(
LinearLayout
)
findViewById
(
R.
id
.
task
)
;
accounts
=
(
LinearLayout
)
findViewById
(
R.
id
.
accounts
)
;
sended
=
(
LinearLayout
)
findViewById
(
R.
id
.
sended
)
;
content
=
(
LinearLayout
)
findViewById
(
R.
id
.
content
)
;
?
taskText
=
(
TextView
)
findViewById
(
R.
id
.
taskText
)
;
accountsText
=
(
TextView
)
findViewById
(
R.
id
.
accountsText
)
;
sendedText
=
(
TextView
)
findViewById
(
R.
id
.
sendedText
)
;
?
LayoutInflater factory
=
LayoutInflater.
from
(
this
)
;
View
taskView
=
factory.
inflate
(
R.
layout
.
task
,
null
)
;
View
accountsView
=
factory.
inflate
(
R.
layout
.
accounts
,
null
)
;
View
sendedView
=
factory.
inflate
(
R.
layout
.
sended
,
null
)
;
//把三個內容視圖的XML文件轉化成View
?
content.
addView
(
taskView
)
;
//啟動時默認載入taskView
?
task.
setOnClickListener
(
new
TabListener
(
task, taskText, taskView
)
)
;
accounts.
setOnClickListener
(
new
TabListener
(
accounts, accountsText,
accountsView
)
)
;
sended.
setOnClickListener
(
new
TabListener
(
sended, sendedText,
sendedView
)
)
;
//設置三個tab的點擊監聽器
?
}
?
class
TabListener
implements
OnClickListener
{
LinearLayout layout
;
TextView tv
;
View
subView
;
?
TabListener
(
LinearLayout layout, TextView tv,
View
subView
)
{
this
.
layout
=
layout
;
this
.
tv
=
tv
;
this
.
subView
=
subView
;
}
?
@Override
public
void
onClick
(
View
v
)
{
// TODO Auto-generated method stub
task.
setBackgroundDrawable
(
null
)
;
accounts.
setBackgroundDrawable
(
null
)
;
sended.
setBackgroundDrawable
(
null
)
;
taskText.
setTextColor
(
getResources
(
)
.
getColor
(
R.
color
.
green
)
)
;
accountsText.
setTextColor
(
getResources
(
)
.
getColor
(
R.
color
.
green
)
)
;
sendedText.
setTextColor
(
getResources
(
)
.
getColor
(
R.
color
.
green
)
)
;
// 全部設為未選中狀態
?
layout.
setBackgroundResource
(
R.
drawable
.
textback
)
;
tv.
setTextColor
(
getResources
(
)
.
getColor
(
R.
color
.
white
)
)
;
// 設置選中項
?
content.
removeAllViews
(
)
;
//移除所有內容
content.
addView
(
subView
)
;
//添加傳入的View
?
}
?
}
}
|
Strings.xml中存兩個顏色值:
1
2
|
<color
name
=
"white"
>
#ffffff
</color
>
<color
name
=
"green"
>
#0cc054
</color
>
|
三個內容視圖的xml,只貼一個,另兩個一樣,名字不同而已:
task.xml:
1
2
3
4
5
6
7
8
|
<?xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<TextView
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:text
=
"Task"
>
</TextView
>
|
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

