【问题标题】:Toolbar and TabLayout is not visible on Android 4.4 devices工具栏和 TabLayout 在 Android 4.4 设备上不可见
【发布时间】:2015-07-10 15:09:59
【问题描述】:

我试图通过参考 [http://blog.grafixartist.com/material-design-tabs-with-android-design-support-library/] [博客] 使用谷歌设计库来实现 Toolbar 和 TabLayout。

输出在 Lollipop 设备上按预期工作,但在 Kitkat 设备上不显示 ToolBar 和 TabLayout。但我仍然可以在 kitkat 设备上按预期滑动 3 个片段。为什么使用谷歌支持库编写的相同代码在不同设备上的工作方式不同!

我尝试参考 [Toolbar is not visible on Android 4.X devices [solved] 问题,但没有解决问题。我尝试使用 API 19 在模拟器中运行代码,但也面临同样的问题。

我在项目中添加了'com.android.support:appcompat-v7:22.2.0''com.android.support:support-v4:22.2.0''com.android.support:design:22.2.0'依赖项。

activity_main.xml

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/tools">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/Base.ThemeOverlay.AppCompat.Dark" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

MainActivity.java

public class MainActivity extends AppCompatActivity {

    public ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
        tabLayout.setupWithViewPager(viewPager);

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new QueuedFragment(), "Queued");
        adapter.addFrag(new IntransitFragment(), "InTransit");
        adapter.addFrag(new DeliveredFragment(), "Delivered");
        viewPager.setAdapter(adapter);
    }
}

style.xml

<resources>

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
</style>

【问题讨论】:

  • 你能发布你的样式文件吗?
  • 我添加了样式文件。如果您需要更多信息,请告诉我。
  • 为什么TabLayout的高度是match_parent
  • 我尝试将其更改为 wrap_content 但仍然面临同样的问题。
  • 你有解决办法吗?

标签: android android-viewpager toolbar android-appcompat android-tablayout


【解决方案1】:

这发生在我身上,因为我使用的是 CoordinatorLayout,但我没有指定内容的 layout_behavior。所以在我的情况下你应该这样做:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cl_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:ignore="MissingPrefix">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="@color/white"
            app:titleEnabled="false">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/white"
                android:elevation="4dp"
                app:layout_collapseMode="pin">

                <TextView
                    android:id="@+id/toolbar_title"
                    fontPath="fonts/Medium-Extd.otf"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="my toolbar title"
                    android:textAllCaps="true"
                    android:textColor="#000000"
                    android:textSize="14sp" />

            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>


<!-- add the behavior here and the toolbar apppeared on 4.4 devices in my case -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >

//...... blah blah

        </RelativeLayout>

【讨论】:

    【解决方案2】:

    我在 Android 4 中遇到了问题,我解决了。怎么看。 需要将工具栏移到更高的级别。


    <?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:fitsSystemWindows="true"
        >
        <!-- Framelayout to display Fragments -->
        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/transparent"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
    
        </android.support.design.widget.AppBarLayout>
    
    
    
        <!--<android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@android:drawable/ic_dialog_email" />-->
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

      【解决方案3】:
      <android.support.design.widget.CoordinatorLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          xmlns:app="http://schemas.android.com/tools">
      
          <android.support.design.widget.AppBarLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
      
              <android.support.v7.widget.Toolbar
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="?attr/actionBarSize"
                  android:background="?attr/colorPrimary"
                  app:layout_scrollFlags="scroll|enterAlways"
                  app:popupTheme="@style/ThemeOverlay.AppCompat.Light"//here
                  android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
      
              <android.support.design.widget.TabLayout
                  android:id="@+id/tablayout"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent" />
      
          </android.support.design.widget.AppBarLayout>
      
          <android.support.v4.view.ViewPager
              android:id="@+id/viewpager"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior" />
      
      </android.support.design.widget.CoordinatorLayout>
      

      还有:

      <resources>
          <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
              <item name="windowActionBar">false</item>
              <item name="windowActionBarOverlay">false</item>
              <item name="windowNoTitle">true</item>
          </style>
      </resources>
      

      或:

       <style name="ParallaxTheme" parent="Theme.AppCompat.Light.NoActionBar">
          <item name="windowActionBar">false</item>
          <item name="windowNoTitle">true</item>
      </style>
      

      【讨论】:

      • 感谢 SilentKnight 在 style.xml 文件中添加 windowActionBarOverlay 属性后,它现在工作正常。
      【解决方案4】:

      如果 activity_main.xml 中的顶级布局父级不是 CoordinatorLayout(是否有 FrameLayout 或 RelativeLayout?)根据 API 版本可能会有一些错误的不同重叠。 将顶部布局父级更改为 CoordinatorLayout,它将在任何地方工作。

      【讨论】:

      • 顶部布局仅是 CoordinatoLayout,但由于一些编辑问题,它之前没有显示。请您现在检查一下,如果有问题请告诉我
      • xmlns:app="http://schemas.android.com/tools"更改为xmlns:app="http://schemas.android.com/apk/res-auto"
      • 我把它改成了 "xmlns:app="schemas.android.com/apk/res-auto" 但我仍然面临同样的问题,在里面添加了 "false" 行style.xml 文件,正如 SilentKnight 所建议的,它现在工作正常。
      • 在我看来,app: 前缀的错误命名空间会产生重叠。
      • 即使在更改应用前缀后我也面临同样的问题,所以我认为这不是因为那个。但我再次面临这个问题,我可能不得不再次发布另一个问题。
      【解决方案5】:

      组件树 -> 选项卡布局或工具栏 -> 转换为 ConstraintLayout 即可使用

      【讨论】:

      • 请详细说明您的answer 并提供一些解释。
      猜你喜欢
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多