【问题标题】:Android TabLayout not visible on my applicationAndroid TabLayout 在我的应用程序中不可见
【发布时间】:2016-08-27 05:18:56
【问题描述】:

我正在尝试在我的应用程序上添加简单的 TabLayout,但它不可见,查看寻呼机工作正常

这个布局是toolbar_layout

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:titleTextColor="#ffffff"
        app:layout_scrollFlags="scroll|enterAlways">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabsLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMode="fixed"/>
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

我的主要布局是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/content_background">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include layout="@layout/application_toolbar"/>

            <android.support.v4.view.ViewPager
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="-25dp"
                android:layout_marginRight="-25dp"
                />
        </LinearLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigationView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/header"
            app:menu="@menu/drawer"/>

        <com.lapism.searchview.SearchView
            android:id="@+id/searchView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"/>
    </android.support.v4.widget.DrawerLayout>
</android.support.design.widget.CoordinatorLayout>

正如我所说,查看寻呼机工作正常,但标签在我的应用程序上不可见

【问题讨论】:

    标签: android


    【解决方案1】:

    TabLayout 不应在 Toolbar 中。

    检查this repository作为使用CoordinatorLayout、DrawerLayout、Toolbar、TabLayout的示例。

    这里是activity_main.xml 和include_list_view.xml。

    视图层次结构是这样的:

    DrawerLayout
     - CoordinatorLayout
       - AppBarLayout
         - Toolbar
         - TabLayout
       - ViewPager
     - NavigationView
    

    【讨论】:

      【解决方案2】:

      主布局

              <?xml version="1.0" encoding="utf-8"?>
          <android.support.design.widget.CoordinatorLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@drawable/content_background">
      
              <android.support.v4.widget.DrawerLayout
                  android:id="@+id/drawerLayout"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
      
                  <LinearLayout
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:orientation="vertical">
      
                      <include layout="@layout/application_toolbar"/>
      
                      <android.support.v4.view.ViewPager
                          android:id="@+id/container"
                          android:layout_width="match_parent"
      <!-- Change this -->
                          android:layout_height="0dp"
                          android:layout_weight="1"
      <!-- Code -->
                          android:layout_marginLeft="-25dp"
                          android:layout_marginRight="-25dp"
                          />
                  </LinearLayout>
      
                  <android.support.design.widget.NavigationView
                      android:id="@+id/navigationView"
                      android:layout_width="wrap_content"
                      android:layout_height="match_parent"
                      android:layout_gravity="start"
                      app:headerLayout="@layout/header"
                      app:menu="@menu/drawer"/>
      
                  <com.lapism.searchview.SearchView
                      android:id="@+id/searchView"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:visibility="gone"/>
              </android.support.v4.widget.DrawerLayout>
          </android.support.design.widget.CoordinatorLayout>
      

      LinearLayout 的android:layout_height 设置为匹配隐藏TabLayout 的父级。将android:layout_height 更改为0dp 并将android:layout_weight 设置为1。我认为这可能会解决您的问题。

      【讨论】:

        【解决方案3】:

        那是因为您将 TabLayout 放入了 Toolbar。
        尝试将您的 toolbar_layout 更改为:

        <android.support.design.widget.AppBarLayout>    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                android:titleTextColor="#ffffff"
                app:layout_scrollFlags="scroll|enterAlways">
        
            </android.support.v7.widget.Toolbar>
        
            <android.support.design.widget.TabLayout
                    android:id="@+id/tabsLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabGravity="fill"
                    app:tabMode="fixed"/>
        
        </android.support.design.widget.AppBarLayout>
        

        【讨论】:

          【解决方案4】:

          试试这个:

          <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                       android:id="@+id/fragment_container"
                       android:layout_width="match_parent"
                       android:layout_height="match_parent"
                       xmlns:app="http://schemas.android.com/apk/res-auto"
                       xmlns:tools="http://schemas.android.com/tools"
                       tools:context=".MainActivity">
              <android.support.v4.view.ViewPager
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/pager"
                  app:layout_behavior="@string/appbar_scrolling_view_behavior"
                  android:layout_height="match_parent"
                  android:layout_width="match_parent" />
          
              <android.support.design.widget.AppBarLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
          
                  <android.support.v7.widget.Toolbar
                      android:id="@+id/action_bar"
                      android:layout_width="match_parent"
                      android:layout_height="?attr/actionBarSize"
                      android:background="?attr/colorPrimary"
                      android:elevation="4dp"
                      android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
                  <!-- Tabs-->
                  <android.support.design.widget.TabLayout
                      android:id="@+id/tab_layout"
                      app:tabMode="fixed"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:background="?attr/colorPrimary"
                      app:tabTextColor="#d3d3d3"
                      android:minHeight="?attr/actionBarSize"
                      />
              </android.support.design.widget.AppBarLayout>
          
          </FrameLayout>
          

          FrameLayout 是 Activity 的主要内容视图。

          【讨论】:

            【解决方案5】:

            我居然在这里找到了别人的答案。

            Tabs of TabLayout not showing

            tabLayout.post(new Runnable() {
                @Override
                public void run() {
                    tabLayout.setupWithViewPager(viewPager);
                }
            });
            

            希望对你有帮助

            【讨论】:

            • 不要复制另一个 StackOverflow 问题的答案。相反,将此问题标记为重复。或者如果不完全重复,则将 StackOverflow 链接作为评论放在问题下方。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-12
            相关资源
            最近更新 更多