【问题标题】:Android: start an intent into a framelayoutAndroid:将意图启动到框架布局中
【发布时间】:2010-05-03 22:14:28
【问题描述】:

我有这个布局文件的主要活动:

<?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>

基本上它由页眉、中心部分(android:id="@+id/center")和页脚组成。 页脚包含四个动态创建的按钮。最后它看起来像一个 TabWidget,底部有标签。

每个页脚的按钮都包含一个意图/活动。

问题是:如何在 FrameLayout 中启动我的活动? 例如 TabHost 这样做:

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

【问题讨论】:

    标签: android layout android-intent


    【解决方案1】:

    如果要显示的每个页面都有单独的活动,则必须为容器活动(显示选项卡的活动)扩展 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;
        }
    

    注意:要让这些东西正常工作,你必须做很多调整和奇怪的事情(注意他们在评论中引用的错误),这可能是它没有记录的原因。

    【讨论】:

    • 泰!这正是我想要的。在同一视图中处理多个意图的方法似乎不像 TabWidget 那样常见。由于我是 android 的新手,将不同的复杂视图处理成一个通用布局文件的正确方法是什么?也许使用合并?如果我想改变我目前的方法,我是否需要改变 TabOneActivity.java、TabTwoActivity.java 来将 Activity 类扩展为视图?
    猜你喜欢
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多