gpt4 book ai didi

android - Android 中的选项卡,如何创建内容

转载 作者:行者123 更新时间:2023-11-29 00:32:25 25 4
gpt4 key购买 nike

嘿,我对创建 Android 应用程序还很陌生。我想在操作栏中创建三个选项卡。我已经通过使用 New->Android Activity-> Navigation with Swipe and Tabs 创建它们来完成此操作。

这就是我的 MainActivity 代码(使用了 android.com 的教程之一):

 public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";

public DummySectionFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create a new TextView and set its text to the fragment's section
// number argument value.
int current_section = getArguments().getInt(ARG_SECTION_NUMBER);

switch(current_section){
case 1:
{

EditText element1 = new EditText(getActivity());
element1.setHint(R.string.edit_text_hint);
element1.setGravity(Gravity.TOP);
element1.setPadding(40,40,40,0);
element1.setSingleLine();

TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setText("Search");


return element1;
}
case 2:
{
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setText("User Account");
return textView;
}
case 3:
{
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setText("Last Updated");
return textView;
}
}
TextView textView1 = new TextView(getActivity());
textView1.setGravity(Gravity.CENTER);
// textView.setText(Integer.toString(getArguments().getInt(
// ARG_SECTION_NUMBER)));
return textView1;
}
}

所以在三个选项卡中的每一个都有不同的项目显示,不幸的是它只能显示一个 - 而不是更多。我认为这是一个很简单的问题,但我只是不知道这是如何工作的。希望你能帮我解决这个问题。

最佳答案

在本教程中,您将返回一个 textview。尝试将 TextView 添加到布局,并返回布局。

我认为您想要的是对不同的选项卡使用不同的 fragment 。然后,您可以在 fragment 本身中指定您的布局和其他代码,而不是使用 switch case 构建一个大 fragment 。

对于每个 fragment ,您都创建一个新类。此 Fragment 可以添加到 TabBarListener

FragmentonCreateView 中,您可以通过将对象添加到 view 参数以编程方式创建布局。您还可以使用充气器来充气您的 XML 布局。

我创建了一个示例项目,其中 1 个 Fragment View 被放大,另一个以编程方式创建:

here

如果你阅读 android 文档,它会更清楚。我认为他们甚至有一个示例代码项目。

Actiobar, the tab sections

More info about fragments

关于android - Android 中的选项卡,如何创建内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14644217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com