PagerTabStrip not showing in ViewPager in app targeting Android 6.X (N)

一笑奈何 提交于 2019-12-07 10:06:47

问题


I have an existing app that makes use of ViewPager and PagerTabStrip and this has been working for a long time with various releases of the Android SDK and the com.android.support:appcompat-v7 library. I've just bumped up the compile and target SDK versions of my app from 23 to 24 and the com.android.support:appcompat-v7 library version from 23.4.0 to 24.0.0... and I now see that the PagerTabStrip is totally missing from the ViewPager. (Not just missing titles in the PagerTabStrip... the PagerTabStrip is totally missing from the ViewPager when the views are rendered.)

This is how I'm declaring the ViewPager and PagerTabStrip views in my layout file:

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

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pagerTabStrip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:textColor="@android:color/white"
        android:background="@color/view_bg_blue" />

</android.support.v4.view.ViewPager>

Can anyone spot something that I'm doing wrong or could this be a bug in ViewPager and PagerTabStrip?


回答1:


This is the temporary workaround until the issue is resolved in the Android support library:

((ViewPager.LayoutParams) pagerTabStrip.getLayoutParams()).isDecor = true;

See here for a fuller discussion on the status of the issue: https://code.google.com/p/android/issues/detail?id=213359




回答2:


We’ll start with the layout for our activity (**activity_pager_tab_strip.xml**), which in this case will just be a ViewPager containing a PagerTabStrip:
[![enter image description here][1]][1]
  [1]: http://i.stack.imgur.com/LUvF9.png

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
public class PagerTabStripActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pager_tab_strip);
        TabsPagerAdapter adapter = new TabsPagerAdapter(getSupportFragmentManager());
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);
    }
}
Example: http://photo-wonder.blogspot.com/2016/09/using-pagertabstrip-with-viewpager.html


来源:https://stackoverflow.com/questions/38037785/pagertabstrip-not-showing-in-viewpager-in-app-targeting-android-6-x-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!