android 應用TabHost優化Activity的GUI
日期:2016/3/13 17:12:34   編輯:開發入門
智能移動設備的操作會受限於屏幕的尺寸。一些有關GUI優化技巧不提倡在單獨的Activity中顯示過多的元素,比較簡單的解決辦法是將其拆分成多個ActivitIEs,每個部分都單獨調用。盡管這樣解決了表面問題,但是卻出現了額外的麻煩,尤其是針對某些內聚屬性較高的操作,這樣會降低操作的流暢性。
這裡介紹一種比較“溫馨”的解決方法,應用Widget.TabHost將多個ActivitIEs合並成一個Activity,這樣就可以同時解決兩個問題: 1) 避免單一Activity包含過多元素而過於臃腫,
2) 而且也解決了將復雜的ActivitIEs拆分後,降低了拆分“強內聚”而影響到系統的整體性。
對於有經驗的朋友會非常容易了解下邊例子中包含的方法所代表的作用,那麼對於剛剛接觸人可以將TabHost理解成類似於浏覽器中用來索引不同頁面的Tab標簽,接下來分析實際的例子。 定義layout:
?VIEw Code XML
<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- androidres.com -->
<TabHost
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="105px"/> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="65px">
<LinearLayout
android:id="@+id/tab1content"
android:orIEntation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00008B"> <!-- Put Tab 1 VIEws in here -->
</LinearLayout> <LinearLayout
android:id="@+id/tab2content"
android:orIEntation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFF00"> <!-- Put Tab 2 VIEws in here --> </LinearLayout>
</FrameLayout>
</TabHost>
</LinearLayout> 例子中的LinearLayout作為每個獨立標簽(TabHost.TabSec)所包含的內容:
1.tabSec.setContent(R.id.tab1content);
同時也可以直接將Intent實體作為參數,這樣可以將外部的Activity類作為當前類實體的一部分:
1.tabSec.setContent(new Intent(this, Tab1Activity.class)));
下邊分別是兩種用於主程序中初始化TabHost的方法,請根據實際的需要選擇恰當的處理方式:
1)應用TabSpec分割Activity內部Layout元素(對應於上邊的Layout實例):
1.TabHost tabs = (TabHost)this.findVIEwById(R.id.tabhost);
2. tabs.setup();
3. 4. TabHost.TabSpec one = tabs.newTabSpec("one");
5. one.setContent(R.id.tab1content);
6. one.setIndicator("TAB 1");
7. tabs.addTab(one);
2)將多個Activity類整合到同一個Activity中:
1.TabHost host = getTabHost();
2.host.addTab(host.newTabSpec("one").setIndicator("TAB1").setContent(new Intent(this, Tab1Activity.class)));
3.host.addTab(host.newTabSpec("two").setIndicator("TAB2").setContent(new Intent(this, Tab2Activity.class)));
其中的Tab1Activity和Tab2Activity分別是兩個獨立的Activity類,上邊的例子中所聲明的實體將被植入TabSpec中。
- 上一頁:Android平台Qt開發入門教程
- 下一頁:如何將android view的位置設為右下角