【问题标题】:Android TabHost - setCurrentTab() does not change the tab contentAndroid TabHost - setCurrentTab() 不会改变标签内容
【发布时间】:2016-09-29 14:52:56
【问题描述】:

我的问题是当我使用 tabHost.setCurrentTab(...) 时选项卡内容没有改变。可以看到,相应的选项卡被标记为活动状态,内容保持不变。当我单击选项卡时,内容会发生变化。

到设置。

我在一个使用片段的应用中拥有不同的功能。一个片段包含一个tabhost。每个选项卡都有自己的布局,片段应该在这些选项卡之间切换,并根据应用程序的状态显示选项卡选择。

我会尝试引用重要的代码段落。

片段的主xml文件:

<TabHost
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabHost"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="fill"
        android:background="@color/colorPrimary"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

            <include layout="@layout/fragment_master_emg_start"
                android:layout_height="445dp"
                android:layout_width="fill_parent"
                android:layout_gravity="center|bottom"/>

           <include layout="@layout/fragment_master_emg_setup"
                android:layout_height="445dp"
                android:layout_width="fill_parent"
                android:layout_gravity="center|bottom"/>

    ... some more includes

    </FrameLayout>
</TabHost>

包含的 xml 文件只包含一个相对布局和一堆按钮和文本视图。

Fragments onCreate 方法:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Get View
    View rootView = inflater.inflate(R.layout.fragment_master_emg, container, false);
    Button button= (Button)rootView.findViewById(R.id.button);
    button.setText("test");
    TabHost tabHost=(TabHost)rootView.findViewById(R.id.tabHost);
    tabHost.setup();
    // Tabs
    // Setup - Start Tab

    TabHost.TabSpec t=tabHost.newTabSpec("Tab0");
    t.setContent(R.id.master_emg_start);
    t.setIndicator("EMG Measurement - Setup");
    tabHost.addTab(t);

    TabHost.TabSpec t0=tabHost.newTabSpec("Tab1");
    t0.setContent(R.id.master_emg_setup);
    t0.setIndicator("Setup");
    tabHost.addTab(t0);

    .... more tabs
    ....

    // Set initial visibility
    tabHost.getTabWidget().getChildAt(0).setVisibility(View.VISIBLE);        
    tabHost.getTabWidget().getChildAt(1).setVisibility(View.GONE);

   // Buttons to switch tabs

   button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TabHost tabHost=(TabHost)getActivity().findViewById(R.id.tabHost);
            tabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);
            tabHost.getTabWidget().getChildAt(1).setVisibility(View.VISIBLE);
            tabHost.getTabWidget().setCurrentTab(1);
        }
    });

我刚刚在这段摘录中包含了两个选项卡,但这将是基本过程。

我试图使选项卡主机无效,以便重新绘制所有内容。这并没有改变什么。

【问题讨论】:

    标签: android android-layout android-fragments android-tabhost android-tabs


    【解决方案1】:

    对于任何登陆此问题解决方案的人:

    使用:

     tabHost.setCurrentTab(tabNumber);
    

    而不是:

     tabHost.getTabWidget().setCurrentTab(tabNumber);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      相关资源
      最近更新 更多