android 自定義TabActivity的實(shí)例方法
一、改變Tab欄的位置。
java代碼。在TabActivity的oncreate方法中添加
setContentView(R.layout.tab_host);
其中 Layout tab_host.xml 是從系統(tǒng)資源文件中摳出來之后略作修改。
系統(tǒng)原來的 tab_host.xml內(nèi)容如下
<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/layout/tab_content.xml
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">
<TabWidget android:id="@android:id/tabs" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight="0" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="match_parent" android:layout_height="0dip"
android:layout_weight="1"/>
</LinearLayout>
</TabHost>
要實(shí)現(xiàn)TAB欄在頁面下方,只需簡單修改。
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="0dip"
android:layout_weight="1"/>
<TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="0" />
</LinearLayout>
</TabHost>
這樣,就實(shí)現(xiàn)了TAB欄在頁面下冊(cè)。需要注意的是,view的id不要修改。
二、自定義TAB的圖片。系統(tǒng)自帶的tab_indicator.xml內(nèi)容如下
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="64dip"
android:layout_weight="1"
android:layout_marginLeft="-3dip"
android:layout_marginRight="-3dip"
android:orientation="vertical"
android:background="@android:drawable/tab_indicator">
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/tabWidgetStyle"
/>
</RelativeLayout>
可以看出,默認(rèn)情況下,圖標(biāo)在文字上方,并且不能占到整個(gè)格,無法滿足設(shè)計(jì)需要。因此可以重寫該Layout。
編寫tab_in.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="64dip"
android:orientation="vertical"
>
</RelativeLayout>
View view1 = inflater.inflate(R.layout.tab_in, null);;
View view2 = inflater.inflate(R.layout.tab_in, null);;
View view3 = inflater.inflate(R.layout.tab_in, null);;
view1 .setBackgroundResource(R.drawable.record_upload_button_stateful);
view2 .setBackgroundResource(R.drawable.record_download_button_stateful);
view3 .setBackgroundResource(R.drawable.record_receive_button_stateful);
tabHost.addTab(tabHost
.newTabSpec("view1")
.setIndicator(view1)
);
tabHost.addTab(tabHost
.newTabSpec("view2")
.setIndicator(view2)
);
tabHost.addTab(tabHost
.newTabSpec("view3")
.setIndicator(view3)
);
- Android使用Theme自定義Activity進(jìn)入退出動(dòng)畫的方法
- Android開發(fā)技巧之在a標(biāo)簽或TextView控件中單擊鏈接彈出Activity(自定義動(dòng)作)
- Android實(shí)現(xiàn)將一個(gè)Activity設(shè)置成窗口樣式的方法
- Android基礎(chǔ)之Fragment與Activity交互詳解
- Android的Activity跳轉(zhuǎn)動(dòng)畫各種效果整理
- android的activity跳轉(zhuǎn)到另一個(gè)activity
- Android Activity之間傳遞圖片(Bitmap)的方法
- Android實(shí)現(xiàn)Activity界面切換添加動(dòng)畫特效的方法
- Android 多個(gè)Activity之間的傳值
- Android實(shí)現(xiàn)可使用自定義透明Dialog樣式的Activity完整實(shí)例
相關(guān)文章
Android中Fragmen首選項(xiàng)使用自定義的ListPreference的方法
Android中Fragmen的首選項(xiàng)可以使用自定義的ListPreference,這樣Fragment的PreferenceFragment就可以更方便地保存配置信息,需要的朋友可以參考下2016-05-05android studio 一直卡在Gradle:Build Running的幾種解決辦法
這篇文章主要介紹了android studio 一直卡在Gradle:Build Running的解決辦法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10Android實(shí)現(xiàn)自定義驗(yàn)證碼輸入框效果(實(shí)例代碼)
這篇文章主要介紹了Android實(shí)現(xiàn)自定義驗(yàn)證碼輸入框效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01Android?Activity生命周期調(diào)用的理解
大家好。本篇文章主要講的是Android?Activity生命周期調(diào)用的理解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Android實(shí)現(xiàn)快遞物流跟蹤布局效果
本篇文章主要介紹了Android實(shí)現(xiàn)快遞跟蹤布局效果,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05Android編程實(shí)現(xiàn)禁止StatusBar下拉的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)禁止StatusBar下拉的方法,涉及Android StatusBarManager相關(guān)屬性控制操作技巧,需要的朋友可以參考下2017-08-08AndroidManifest.xml uses-feature功能詳解
這篇文章主要介紹了AndroidManifest.xml uses-feature功能,較為詳細(xì)的分析了Android屬性過濾操作的功能與相關(guān)技巧,需要的朋友可以參考下2016-10-10