android - 工具栏和 TabLayout 在 Android 4.4 设备上不可见

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

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

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

我试着引用 [ 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>

最佳答案

这发生在我身上,这是因为我使用的是 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>

关于android - 工具栏和 TabLayout 在 Android 4.4 设备上不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31344288/

相关文章:

android - Android Studio Gradle Analyze已损坏

android - 可以混淆/加密 SharedPreferences 文件吗?

android - 来自另一个 fragment/Activity 后 fragment 不显示

android - 嵌套 fragment 是否可能?

android - 半透明/透明状态栏 + CoordinatorLayout + Toolbar + Fragment

android - 如何购买和消费个人币?

android - ViewPager 禁止向某个方向滑动

android - 带有图像、选项卡和 FAB 的折叠工具栏

android - 协调器布局内容显示在状态栏后面

javascript - 手机上的谷歌地图 rtl 提供水平滚动