1,這個問題和這個是同樣的問題:
PagerTabStrip not showing in ViewPager in app targeting Android 6.X (N)
目標是Android N的應用, 需要使用下面的解決辦法:
在 onCreateView(), 添加:
((ViewPager.LayoutParams) (view.findViewById(R.id.tab_strip)).getLayoutParams()).isDecor = true;
2,把下面這行從 tab1.onCreateView(...)
.方法中移除..
((ViewPager.LayoutParams) (view.findViewById(R.id.tab_strip)).getLayoutParams()).isDecor = true;
... 添加它到 MainActivity.onCreate(...)
中.
app 就不會出錯了.
你的 tab1
Fragment
不會知道 R.id.tab_strip
view控件.
這是 MainActivity
class的控件.
|
我一個現有的app,使用了ViewPager和PagerTabStrip,它們都是使用多版本的Android SDK 和
下面是聲明的 <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>
這是代碼寫錯了還是 |
|
這有一個臨時的解決方案,直到支持庫解決了這個 issue: ((ViewPager.LayoutParams) pagerTabStrip.getLayoutParams()).isDecor = true; 看這裏有這個 issue更全面的討論:https://code.google.com/p/android/issues/detail?id=213359 |
Issue 213359: | PagerTabStrip在 com.android.support library 24.0.0上不工作 |
我把項目sdk版本更新到 24,支持庫也同時更新了 and therefore upgraded my support libraries.
這時PagerTabStrip不出現了.
甚至把項目選項改 "Tabbed activity" 作爲默認的啓動activity ,在 ViewPager添加 PagerTabStrip 還是不顯示
e.g.
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<android.support.v4.view.PagerTabStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
android:layout_gravity="top"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:textColor="#000000" />
</android.support.v4.view.ViewPager>
但是當支持庫再用 23.3.0 它又會顯示.
我是在 nexus 6 運行的 N preview 4 預覽版測試的.
不完全是. Annotations 也可以inherited(遺傳), 但是我們沒有添加 @Inherited 標識到 這個annotation.
我的意思是說默認是不能inherited .你提到的這個標識 (flag)當然也可以解決這個問題.
正確的解決方案: ((ViewPager.LayoutParams) tabStrip.getLayoutParams()).isDecor = true; 先來說它的工作原理. 當把一個一個View添加到一個ViewPager的時候,它會獲取它的佈局參數並且更新他們 , 包括決定View是不是應該作爲裝飾顯示的 isDecor 參數. 先前,所有實現ViewPager.Decor接口的View都是由ViewPager 設置的. PagerTitleStrip 實現了它,而且繼承PagerTabStrip 擴展了它,運行正常.
在24.0.0 ,這個接口被這個annotation(註解) ViewPager.DecorView 取代了,不知道什麼原因,並且這個annotation需要由view的類型處理. 所以現在 PagerTitleStrip 是annotated(註解過的) ,而不是實現了這個接口, 但是 Android developers 忘記在PagerTabStrip添加相同的 annotation,並且這樣的 annotations也是不能由inherited(遺傳)得到的 , 就不再有作用了.
解決辦法是隻需要手動設置一下參數. 其實在 XML文件裏設置它會更好,但遺憾的是沒有相應的 XML 屬性, 所以我們只好在代碼中去設置.