Android 选项卡:活动还是视图?

发布于 2024-11-27 03:22:17 字数 1431 浏览 1 评论 0原文

我正在尝试编写一个 Android 小应用程序来存储食谱作为练习。要添加新食谱,我正在尝试使用选项卡。有三个选项卡,

  1. 成分
  2. 步骤
  3. 媒体

我希望“成分”有一个列表视图,“步骤”有一个列表视图,“媒体”有一个图像库。我将通过选项菜单中的某些选项添加新的成分、步骤和媒体。完成后,我想将所有内容写入 SQLite 数据库。

我想知道是否应该为每个选项卡使用不同的活动或不同的视图?

如果我使用不同的活动,活动之间传递信息是否会很困难?

如果我使用不同的视图,这些视图是否都必须属于同一布局文件?例如,TabWidgets 教程执行以下操作

<FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView 
            android:id="@+id/textview1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:text="this is a tab" />
        <TextView 
            android:id="@+id/textview2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:text="this is another tab" />
        <TextView 
            android:id="@+id/textview3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:text="this is a third tab" />
</FrameLayout>

:所有 TextView 都位于同一个 FrameLayout 中,每个 TextView 仅用于一个选项卡。我认为如果视图更复杂,例如某些东西嵌套在其他东西中,而另一些东西又嵌套在其他东西中,那么这会有点令人困惑。为每个选项卡使用不同的活动,我可以为每个选项卡指定不同的布局文件。

I'm trying to write a little Android application to store recipes as practice. To add a new recipe, I'm trying to use tabs. There are three tabs,

  1. Ingredients
  2. Steps
  3. Media

I'd like "Ingredients" to have a ListView, "Steps" to have a ListView, and "Media" to have a gallery of images. I'm going to add new ingredients, steps, and media through some option in the options menu. When finished, I'd like to write everything to an SQLite database.

I'm wondering if I should use different activities or just different views for each tab?

If I use different activities, would it be hard to pass information between the activities?

If I use different views, do the views all have to be part of the same layout file? For example, the tutorial on TabWidgets does the following:

<FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView 
            android:id="@+id/textview1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:text="this is a tab" />
        <TextView 
            android:id="@+id/textview2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:text="this is another tab" />
        <TextView 
            android:id="@+id/textview3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:text="this is a third tab" />
</FrameLayout>

So all the TextViews are within the same FrameLayout, with each TextView being used for only one tab. I think this would be a little confusing if the views were more complex, e.g. something nested within something else, which is again nested within something else. Using different activities for each tab, I can specify a different layout file for each tab.

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

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

发布评论

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

评论(2

空城旧梦 2024-12-04 03:22:17

我想知道是否应该为每个选项卡使用不同的活动或不同的视图?

使用活动作为选项卡内容已被弃用。您仍然可以这样做,但如果您刚刚开始这个项目,那么使用视图将是更好的方法。

如果我使用不同的活动,活动之间传递信息是否会很困难?

是也不是,但这取决于您的专业水平 - 在活动之间传递信息通常涉及使用意图(尽管还有其他方式)。尽管无论它们是独立的还是选项卡式环境的一部分,这都是相同的,没有任何区别,但确实需要一些思考。

如果我使用不同的视图,这些视图是否都必须属于同一布局文件?

否,但在开始使用 TabActivity 时,您可能想尝试该教程中的方法。

可以有不同的布局(对于任何类型的活动)并根据需要自行扩展它们,但这是一个更复杂的主题。

I'm wondering if I should use different activities or just different views for each tab?

Using Activities as Tab content has been deprecated. You can still do it but if you're just starting out with this project then using Views instead would be a better approach.

If I use different activities, would it be hard to pass information between the activities?

Yes and no, but it depends on your level of expertise - passing information between Activities generally involves using Intents (although there are other ways). Although this is the same whether they're stand-alone or part of a tabbed environment doesn't make any difference but it does take some thinking about.

If I use different views, do the views all have to be part of the same layout file?

No but you might want to experiment with the approach in that tutorial when using a TabActivity to get started.

It's possible to have different layouts (for any type of Activity) and inflate them yourself as necessary but that's a more complex topic.

没有心的人 2024-12-04 03:22:17

我不确定哪个更好,但根据您关于在不同活动之间传递信息是否困难的问题,做起来相当简单。

如果您有两个活动,活动A和活动B,并且想要从A发送信息到B,那么您可以使用以下

Intent intent = new Intent(activityAClassName.this, activityBClassName.class);
intent.putExtra("keyName", "value");
startActivity(intent);

Then从活动A获取活动B中的值,并将此代码发送到活动B

Bundle bundle = this.getIntent().getExtras();
string value = bundle.getString("keyName");

希望这有帮助

I'm not sure which is better but based on your question about whether it is hard to pass information between different activities is fairly simple to do.

If you have two activities, activityA and activityB and you want to send information from A to B then you can use the following

Intent intent = new Intent(activityAClassName.this, activityBClassName.class);
intent.putExtra("keyName", "value");
startActivity(intent);

Then to get the values from activityA in activityB and this code into activityB

Bundle bundle = this.getIntent().getExtras();
string value = bundle.getString("keyName");

hope this helps

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