使用片段在Android上的标签,但在另一个布局 [英] Tabs using Fragments on Android, but inside another Layout

查看:123
本文介绍了使用片段在Android上的标签,但在另一个布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个具有为主要活动表的布局,而该部分作品完美...现在Android应用,当时的想法是增加现有的组件下方的应用程序的其他部分,但现在我要放标签式布局那里。嗯,这部分还完美的作品,当我尝试只是运行。但是,做我必须做这两个这样的方式,这两种显示以下任一另一个非常同一屏幕上混合。

I'm creating android app that has table layout for the main activity, and that part works perfectly... Now, the idea was to add another part of an app below the existing components, but now I have to put a tabbed layout there. Well, that part also works perfectly when I try to run just that. But what do I have to do to mix those two in such a way that these two show up one below another on the very same screen.

我的主要code是:

package my.android;

import android.os.Bundle;

public class MyActivity extends FragmentActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

我对所有的标签不同的布局文件和我有我创建这里的教程下面我TabsActivity类:
http://thepseudo$c$cr.word$p$pss.com/2011/10/04/android-tabs-the-fragment-way/

那么,如何增加一些TabsActivity TA反对MyActivity?而且它是本内容下面是重要的。提前Thaks ...

So how do I add some TabsActivity ta object to the MyActivity? And it is important to be below the content of this. Thaks in advance...

推荐答案

在理想情况下,这将使用嵌套的片段来完成,而Android不支持呢。这使得pcated的ActivityGroup类去$ P $。你需要扩展的ActivityGroup并推出两个活动顶级活动。

Ideally this would be done using nested Fragments, but Android doesn't support that yet. That leaves the deprecated ActivityGroup class. You'll need a top level activity that extends ActivityGroup and launches the two activities.

下面是如何启动活动,并得到他们的意见:

Here is how you launch the activities and get their views:

final Window w = getLocalActivityManager().startActivity(myTag, myIntent);
final View wd = w != null ? w.getDecorView() : null;
if (  null != wd ) {
    wd.setVisibility(View.VISIBLE);
    wd.setFocusableInTouchMode(true);
}
// TODO: Attach wd to a ViewGroup.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
编辑:下面是一个更完整的解决方案。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Below is a more complete solution.

这是最高级别活动布局:

This is the layout for the top level activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    > 
</LinearLayout>

下面是顶层类:

public class EmbeddedActivityParent extends ActivityGroup {

    private LinearLayout    mRootLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         mRootLayout = (LinearLayout) findViewById(R.id.root_layout);

         // Add embedded status activity.
         embedActivity("StatusColumn", new Intent(this, StatusActivity.class));

         // Add embedded work activity.
         embedActivity("WorkArea", new Intent(this, MainActivity.class));
    }

    private void embedActivity(String myTag, Intent launchIntent) {
         final Window w = getLocalActivityManager().startActivity(myTag, launchIntent);
         final View wd = w != null ? w.getDecorView() : null;
         if (  null != wd ) {
             wd.setVisibility(View.VISIBLE);
             wd.setFocusableInTouchMode(true);

             mRootLayout.addView(wd);
         }
    }
}

只要你想你可以添加任意多的嵌入式活动。你甚至可以嵌入鸟巢的活动,但要注意的性能可能会成为一个因素。我们用它来支持动态状态列。

You can add as many embedded activities as you want. You can even nest embedded activities, but be aware that performance could become a factor. We use this to support a dynamic status column.

就个人而言,我认为仍是一个的ActivityGroup使用和希望Gooogle企业改变心意去precating它。

Personally, I think there is still a use for the ActivityGroup and hope that Gooogle changes their mind about deprecating it.

这篇关于使用片段在Android上的标签,但在另一个布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆