標籤:des android style blog http io ar color os
今天這篇文章記述一下頁面頂部底部上下均有Tab標籤頁的特殊需求!使用了過時的ActivityGroup。
再看一下整個Project的結構,如下
下面逐一介紹一下實現過程,一貫風格,具體實現還是看注釋吧,代碼也不是很多,就不囉嗦了。
step1:首先是主介面MainActivity.java
package sun.geoffery.tabtopbottom;import android.app.ActivityGroup;import android.content.Intent;import android.graphics.Color;import android.os.Bundle;import android.view.Window;import android.widget.TabHost;import android.widget.TabHost.TabSpec;import android.widget.TabWidget;import android.widget.TextView;/** * All rights Reserved, Designed By GeofferySun * * @Title: MainActivity.java * @Package sun.geoffery.tabtopbottom * @Description:上下都有Tab的介面 * @author: GeofferySun * @date: 2014-12-9 下午3:41:04 * @version V1.0 */public class MainActivity extends ActivityGroup {// 定義一個TabHost控制項private TabHost mTabHost;public void onCreate(Bundle savedInstanceState) {// 設定隱藏標題列requestWindowFeature(Window.FEATURE_NO_TITLE);super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 擷取TabHost布局mTabHost = (TabHost) findViewById(R.id.tabhost);mTabHost.setup(this.getLocalActivityManager());TabSpec _tab;_tab = mTabHost.newTabSpec("home");_tab.setIndicator("首頁", getResources().getDrawable(R.drawable.ic_launcher));_tab.setContent(new Intent(this, HomeActivity.class));mTabHost.addTab(_tab);_tab = mTabHost.newTabSpec("order");_tab.setIndicator("訂單", getResources().getDrawable(R.drawable.ic_launcher));_tab.setContent(new Intent(this, OrderActivity.class));mTabHost.addTab(_tab);_tab = mTabHost.newTabSpec("wallet");_tab.setIndicator("錢包", getResources().getDrawable(R.drawable.ic_launcher));_tab.setContent(new Intent(this, WalletActivity.class));mTabHost.addTab(_tab);// 設定第一個標籤頁被選中mTabHost.setCurrentTab(0);TabWidget tabWidget = mTabHost.getTabWidget();for (int i = 0; i < tabWidget.getChildCount(); i++) {TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);tv.setTextColor(Color.LTGRAY);// 設定Tab欄字型的顏色}}}
step2:首頁面對應的布局檔案
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- 選項卡布局必須要用TabHost --> <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 鑲嵌線性布局 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- 幀布局 --> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <!-- TabWidget是Tab標籤的使用 --> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0" android:background="#404040"/> </LinearLayout> </TabHost></LinearLayout>
step3:普通的單獨頁面HomeActivity.java(和WalletActivity.java),什麼也沒做,只載入了個簡單的布局!
package sun.geoffery.tabtopbottom;import android.app.Activity;import android.os.Bundle;public class HomeActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_simple); }}
step4:普通的單獨頁面HomeActivity.java(和WalletActivity.java)的布局檔案activity_simple.xml,什麼也也沒有!
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFCC00" android:orientation="vertical" > </LinearLayout>
step5:
重點來了,頂部也要加上Tab標籤的頁面OrderActivity.java。
package sun.geoffery.tabtopbottom;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.widget.TabHost;import android.widget.TabWidget;import android.widget.TextView;/** * All rights Reserved, Designed By GeofferySun * @Title: OrderActivity.java * @Package sun.geoffery.tabtopbottom * @Description:頂部Tab頁面 * @author:GeofferySun * @date:2014-12-9 下午5:31:00 * @versionV1.0 */public class OrderActivity extends Activity {private TabHost mTabHost; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab); mTabHost = (TabHost) findViewById(R.id.mytabhost); mTabHost.setup();TabWidget tabWidget = mTabHost.getTabWidget();mTabHost.addTab(mTabHost.newTabSpec("待服務").setIndicator("待服務").setContent(R.id.page0));mTabHost.addTab(mTabHost.newTabSpec("已完成").setIndicator("已完成").setContent(R.id.page1));mTabHost.addTab(mTabHost.newTabSpec("已取消").setIndicator("已取消").setContent(R.id.page2));int height =120; // int width =45; for(int i=0;i<tabWidget.getChildCount();i++){// 設定高度、寬度,由於寬度設定為fill_parent,在此對它沒效果tabWidget.getChildAt(i).getLayoutParams().height=height;// 設定tab中標題文字的顏色,不然預設為黑色 final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);tv.setTextColor(Color.LTGRAY);}}}
step6:頂部Tab頁面對應的布局檔案activity_tab.xml。
<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mytabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#404040" /> <!-- 注意FrameLayout/TabWidget標籤的位置 --> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" > <TextView android:id="@+id/page0" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#6699FF" android:text="待服務頁面" > </TextView> <TextView android:id="@+id/page1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF9900" android:text="已完成頁面" > </TextView> <TextView android:id="@+id/page2" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#99cc33" android:text="已取消頁面" > </TextView> </FrameLayout> </LinearLayout></TabHost>
step7:最後是資訊清單檔Manifest.xml。
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="sun.geoffery.tabtopbottom" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".OrderActivity"></activity> <activity android:name=".HomeActivity"></activity> <activity android:name=".WalletActivity"></activity> </application></manifest>
到此為止,全部代碼就可以玩了!
採集
Android 底部TabActivity(2)——ActivityGroup