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
|
<
TabHost
android:id
=
"@android:id/tabhost"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:layout_alignParentLeft
=
"true"
android:layout_alignParentTop
=
"true"
>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
>
<
FrameLayout
android:id
=
"@android:id/tabcontent"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:layout_weight
=
"1"
>
</
FrameLayout
>
<
TabWidget
android:id
=
"@android:id/tabs"
android:layout_width
=
"match_parent"
android:layout_height
=
"60dp"
android:layout_weight
=
"0"
android:background
=
"@drawable/bottom_bar"
>
</
TabWidget
>
</
LinearLayout
>
</
TabHost
>
|
1
2
3
4
|
<
selector
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
item
android:state_selected
=
"true"
android:drawable
=
"@drawable/tab_weixin_pressed"
></
item
>
<
item
android:drawable
=
"@drawable/tab_weixin_normal"
></
item
>
</
selector
>
|
1
2
3
|
<
selector
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
item
android:state_selected
=
"true"
android:drawable
=
"@drawable/tab_bg"
></
item
>
</
selector
>
|
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
|
public
class
MainActivity
extends
android.app.TabActivity
{
//继承TabActivity
String[] name = {
"微信"
,
"通讯录"
,
"朋友们"
,
"设置"
};
//写好四个selector文件,内容为选中状态和默认状态时的图片
int
[] imaResid = {R.drawable.selector_tab1,R.drawable.selector_tab2,R.drawable.selector_tab3,R.drawable.selector_tab4};
ArrayList<TabSpec> arrayList =
new
ArrayList<TabHost.TabSpec>();
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
TabHost tabHost = getTabHost();
for
(
int
i =
0
; i <
4
; i++)
{
//复用布局,创建四个View的对象,并且分别设置View对象里的控件值和图片
View tabrl = getLayoutInflater().inflate(R.layout.tab_item,
null
);
TextView tv_name = (TextView) tabrl.findViewById(R.id.tv_name);
tv_name.setText(name[i]);
ImageView iv_icon = (ImageView) tabrl.findViewById(R.id.iv_icon);
iv_icon.setImageResource(imaResid[i]);
TabSpec spec = tabHost.newTabSpec(
"tab"
+ i).setIndicator(tabrl);
arrayList.add(spec);
}
tabHost.addTab(arrayList.get(
0
).setContent(
new
Intent(MainActivity.
this
,Tab1Activity.
class
)));
tabHost.addTab(arrayList.get(
1
).setContent(
new
Intent(MainActivity.
this
,Tab2Activity.
class
)));
tabHost.addTab(arrayList.get(
2
).setContent(
new
Intent(MainActivity.
this
,Tab3Activity.
class
)));
tabHost.addTab(arrayList.get(
3
).setContent(
new
Intent(MainActivity.
this
,Tab4Activity.
class
)));
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tab, menu);
return
true
;
}
}
|