工具栏和 TabLayout 在 Android 4.4 设备上不可见 [英] Toolbar and TabLayout is not visible on Android 4.4 devices

查看:19
本文介绍了工具栏和 TabLayout 在 Android 4.4 设备上不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过参考 [http://blog.grafixartist.com/material-design-tabs-with-android-design-support-library/] [博客].

I was trying to implement Toolbar and TabLayout using google design library by referring to [http://blog.grafixartist.com/material-design-tabs-with-android-design-support-library/] [blog].

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

Output works as expected on Lollipop devices but it doesnot show ToolBar and TabLayout on Kitkat devices. But i can still swipe through 3 fragment as expected on kitkat device as well. How come same code written using google support libraries works differently on different devices!

我尝试参考 [工具栏不是在 Android 4.X 设备上可见 [已解决] 问题,但它没有解决问题.我尝试使用 API 19 在模拟器中运行代码,但也面临同样的问题.

I tried referring to [Toolbar is not visible on Android 4.X devices [solved] question but it didn't solve the problem. I tried running the code in emulator with API 19 but facing same issue on that as well.

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

I have added 'com.android.support:appcompat-v7:22.2.0', 'com.android.support:support-v4:22.2.0' and 'com.android.support:design:22.2.0' dependencies in the project.

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>

推荐答案

<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>

这篇关于工具栏和 TabLayout 在 Android 4.4 设备上不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆