Android 上使用 Fragment 的选项卡,但位于另一个布局内

发布于 2024-12-13 00:43:38 字数 827 浏览 0 评论 0原文

我正在创建具有主要活动的表格布局的 Android 应用程序,并且该部分工作完美...现在,想法是在现有组件下方添加应用程序的另一部分,但现在我必须在那里放置一个选项卡式布局。嗯,当我尝试运行该部分时,该部分也可以完美运行。但是我必须做什么才能将这两者混合在一起,使这两者在同一个屏幕上显示在另一个下面。

我的主要代码是:

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://thepseudocoder.wordpress.com/2011/ 10/04/android-tabs-the-fragment-way/

那么如何将一些 TabsActivity ta 对象添加到 MyActivity 中?重要的是低于此内容。提前谢谢...

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.

My main code is:

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);
    }
}

I have different layout files for all the tabs and I have my TabsActivity class I have created following the tutorial here:
http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-12-20 00:43:38

理想情况下,这可以使用嵌套片段来完成,但 Android 尚不支持。这就留下了已弃用的 ActivityGroup 类。您将需要一个顶级活动来扩展 ActivityGroup 并启动​​这两个活动。

以下是启动活动并获取他们的意见的方法:

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.

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

这是顶级活动的布局:

<?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);
         }
    }
}

您可以根据需要添加任意数量的嵌入活动。您甚至可以嵌套嵌入式活动,但请注意性能可能成为一个因素。我们用它来支持动态状态列。

就我个人而言,我认为 ActivityGroup 仍然有用,并希望 Google 改变主意,不再弃用它。

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.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Edit: 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>

Here is the top level class:

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文