2010-05-03 56 views
3

我有該佈局文件中的主要活動:機器人:啓動意圖成的FrameLayout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:id="@+id/container" 
    > 
    <LinearLayout 
     android:layout_height="wrap_content" 
     android:id="@+id/header" 
     android:background="@drawable/logo_bk" 
     android:layout_width="fill_parent" 
    > 

    <Button 
     android:id="@+id/btn_reload" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Reload" 
    /> 

    <LinearLayout 
     android:id="@+id/LinearLayout01" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center_vertical|center_horizontal" 
    > 
     <ImageView 
      android:id="@+id/ImageView01" 
      android:src="@drawable/logo_head" 
      android:scaleType="fitStart" 
      android:adjustViewBounds="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

    </LinearLayout> 

    <FrameLayout 
     android:id="@+id/center" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:layout_weight="1"> 
    </FrameLayout> 

    <LinearLayout 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:gravity="center" 
     android:id="@+id/footer" 
     android:layout_weight="2.6" 
     android:background="#ffffff"> 
    </LinearLayout> 

</LinearLayout> 

基本上它是由一個首部,中央部分(機器人:ID =「@ + ID /中心「)和頁腳。 頁腳包含四個動態創建的按鈕。最後它看起來像一個TabWidget,底部有標籤。

每個頁腳的按鈕都包含一個意圖/活動。

問題是:我如何開始我的活動到FrameLayout? 例如TabHost做到這一點:

spec = tabHost 
     .newTabSpec(tabTitle.toLowerCase()) 
     .setIndicator(tabTitle,res.getDrawable(R.drawable.tab_spec)) 
     .setContent(intent); 
     tabHost.addTab(spec); 

回答

6

如果您有您想要顯示的每個頁面獨立的活動,你將不得不延長的ActivityGroup的集裝箱活動(一個顯示選項卡),並LocalActivityManager管理您想要使用的不同嵌入式活動。

這是一種複雜的,沒有記錄。我必須閱讀TabHost的源代碼。

搜索class IntentContentStrategy

基本上,這個想法是,你有一個容器視圖,你使用LocalActivityManager加載活動,得到它的視圖,並將其放置在容器視圖。從TabHost.java

摘錄:

public View getContentView() { 
     if (mLocalActivityManager == null) { 
      throw new IllegalStateException("Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?"); 
     } 
     final Window w = mLocalActivityManager.startActivity(
       mTag, mIntent); 
     final View wd = w != null ? w.getDecorView() : null; 
     if (mLaunchedView != wd && mLaunchedView != null) { 
      if (mLaunchedView.getParent() != null) { 
       mTabContent.removeView(mLaunchedView); 
      } 
     } 
     mLaunchedView = wd; 

     // XXX Set FOCUS_AFTER_DESCENDANTS on embedded activities for now so they can get 
     // focus if none of their children have it. They need focus to be able to 
     // display menu items. 
     // 
     // Replace this with something better when Bug 628886 is fixed... 
     // 
     if (mLaunchedView != null) { 
      mLaunchedView.setVisibility(View.VISIBLE); 
      mLaunchedView.setFocusableInTouchMode(true); 
      ((ViewGroup) mLaunchedView).setDescendantFocusability(
        FOCUS_AFTER_DESCENDANTS); 
     } 
     return mLaunchedView; 
    } 

注:有大量的調整,你必須做的就是這個東西的工作(注意,他們在評論中引用有錯誤)怪異的東西,這可能是爲什麼它沒有記錄。

+0

Ty! 這正是我一直在尋找的。 似乎並不是像TabWidget那樣處理同一個 視圖中的多個意圖的常用方法。 由於我是新的Android的東西,這是正確的方法處理不同的複雜的意見到一個共同的佈局文件?也許使用合併? 如果我想改變我目前的方法,我是否需要改變TabOneActivity.java,將Activity類放到View中的TabTwoActivity.java? – 2010-05-04 09:35:38