【问题标题】:Cannot Create TabLayout in XML无法在 XML 中创建 TabLayout
【发布时间】:2017-08-24 02:33:36
【问题描述】:

我正在尝试在 XML 中创建一个 TabLayout 作为用户个人资料页面。本质上,我想在 3 个单独的选项卡上显示文本,并且文本将根据 Firebase 中的动态数据进行更新。这是我的代码,它没有呈现我的第一个选项卡。我主要针对以下设计:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tabcontent">

    <TabHost android:id="@+id/tab_host"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
            <TabWidget android:id="@android:id/tabs"

                android:layout_width="wrap_content"
                android:layout_height="fill_parent" >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="TEST"/>
                </TabWidget>
    </TabHost>

</FrameLayout>

【问题讨论】:

    标签: android xml android-tabhost


    【解决方案1】:

    渲染期间引发异常:TabHost 需要 FrameLayout id "android:id/tabcontent"。

    这意味着你应该在 TabHost 中使用 FrameLayout,并带有上述 id

    【讨论】:

      【解决方案2】:

      您可以尝试新的android.support.design.widget.TabLayout,简单易用。下面是一个简单的例子,点击 TabItem 切换页面。

      布局

      <android.support.design.widget.TabLayout
          android:id="@+id/main_tablayout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:tabIndicatorHeight="4dp"
          app:tabGravity="fill">
      
          <android.support.design.widget.TabItem
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="table 1"/>
      
          <android.support.design.widget.TabItem
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="table 2"/>
      
          <android.support.design.widget.TabItem
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="table 3"/>
      </android.support.design.widget.TabLayout>
      
      <ViewAnimator
          android:id="@+id/main_viewanimator"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <android.support.v7.widget.RecyclerView
              android:id="@+id/main_recyclerview"
              android:layout_width="match_parent"
              android:layout_height="match_parent"/>
      
          <Button
              android:id="@+id/main_button_pip"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:text="@string/pip_mode"/>
      
          <TextView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center"
              android:text="TEST"/>
      </ViewAnimator>
      

      Java 代码

      mViewAnimator = findViewById(R.id.main_viewanimator);
      mTabLayout = findViewById(R.id.main_tablayout);
      mTabLayout.addOnTabSelectedListener(mTabListen);
      
      private final TabLayout.OnTabSelectedListener mTabListen = new TabLayout.OnTabSelectedListener() {
          @Override
          public void onTabSelected(TabLayout.Tab tab) {
              switch (tab.getPosition()) {
                  case 0:
                      mViewAnimator.setDisplayedChild(0);
                      break;
      
                  case 1:
                      mViewAnimator.setDisplayedChild(1);
                      break;
      
                  case 2:
                      mViewAnimator.setDisplayedChild(2);
                      break;
              }
          }
      
          @Override
          public void onTabUnselected(TabLayout.Tab tab) {
          }
      
          @Override
          public void onTabReselected(TabLayout.Tab tab) {
          }
      };
      

      快照

      【讨论】:

      • 虽然我认为您的答案可能有效,但我本质上是将标签用作 XML,并将在标签内放置动态文本(它将根据与 Firebase 的交互进行更新)。我实际上并不需要 recylerview 功能,选项卡仅显示类似于配置文件屏幕。
      猜你喜欢
      • 1970-01-01
      • 2021-05-23
      • 2019-06-06
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      相关资源
      最近更新 更多