2010-05-03 57 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