2012-08-08 7 views
16

Zamiast wyjaśnienia problemu, dużo łatwiej, jeśli po prostu pokazać wam:Domyślny styl nie działa z TabPageIndicator obiektu ViewPagerIndicator. Dlaczego i jak naprawić?

Unstyled Tab Titles

Jak widać tytuły karcie są wszystkie puree razem i całkowicie pierwotnych ustawień. Działają poprawnie w tym przesuwaniu przez zakładki przełączników (chociaż nie ma widocznych wskazań, z wyjątkiem zmiany pozycji), a dotknięcie zakładki przełącza widok, ale brakuje stylu. Oto kod:

gallerylists.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <com.viewpagerindicator.TabPageIndicator 
    android:id="@+id/indicator" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

    <android.support.v4.view.ViewPager 
    android:id="@+id/gallerypager" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" /> 

</LinearLayout> 

GalleryLists.java

public class GalleryLists extends Activity { 
    Context context; 

    private static final String[] titles = new String[] { 
     "20 Hottest", "20 Worst", "20 Awesomest", "MMA", "Comedy", "Moto", "Games" }; 

    ViewPager listPager; 
    ListPagerAdapter listPagerAdapter; 

    PageIndicator indicator; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gallerylists); 

    context = this; 

    listPagerAdapter = new ListPagerAdapter(); 

    ViewPager.OnPageChangeListener changeListener = new ViewPager.OnPageChangeListener() { 

     @Override 
     public void onPageScrolled(int i, float v, int i1) {} 

     @Override 
     public void onPageSelected(int i) {} 

     @Override 
     public void onPageScrollStateChanged(int i) {} 
    }; 

    listPager = (ViewPager) findViewById(R.id.gallerypager); 
    listPager.setAdapter(listPagerAdapter); 
    listPager.setOnPageChangeListener(changeListener); 

    indicator = (TabPageIndicator) findViewById(R.id.indicator); 
    indicator.setViewPager(listPager); 
    indicator.setOnPageChangeListener(changeListener); 
    } 

    private class ListPagerAdapter extends PagerAdapter { 

    // Not important (I believe) 
    } 
} 

To wszystko. Teraz, jeśli nie jestem bardzo zdezorientowany, mimo przeczytania dokumentacji i zbadania próbek, nie powinienem robić żadnych dodatkowych kroków, aby użyć domyślnego stylu. Jestem trochę zagubiony.

Odpowiedz

11

Czuję się trochę kretynem, ale tutaj jest niezwykle proste i oczywiste rozwiązanie.

AndroidManifest.xml

... 
<activity 
    android:name=".GalleryLists" 
    android:theme="@style/Theme.PageIndicatorDefaults" 
    ... > 
</activity> 
... 

Tak, wszystko, co potrzebne do zrobienia było rzeczywiście użytku tematem. Mam nadzieję, że pomoże to innej biednej zagubionej duszy.

+0

Agggh, miałem tę zakładkę otwartą, ale nigdy jej nie czytałem! W końcu doszedłem do tego, że sam się rozgryzłem, ale mogłem zaoszczędzić czas. – georgiecasey

27

Mam ten sam problem, ale android:theme="@style/Theme.PageIndicatorDefaults" nie łączy się z moim motywem aplikacji.

Jest też inny sposób na personalizację, zastępując res/values/style.xml:

<resources> 

    <style name="AppTheme" parent="android:Theme.Light" > 
     <item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item> 

    </style> 

    <style name="CustomTabPageIndicator" > 
     <item name="android:gravity">center</item> 
     <item name="android:background">@drawable/vpi__tab_indicator</item>   
     <item name="android:paddingTop">12dp</item> 
     <item name="android:paddingBottom">12dp</item> 
     <item name="android:textAppearance">@style/TextAppearance.TabPageIndicator</item> 
     <item name="android:textSize">15sp</item> 
     <item name="android:maxLines">1</item> 
     <item name="android:textColor">#FF555555</item> 
    </style> 
</resources> 
+1

Rzeczywiście wpadłem na podobny problem i zrobiłem coś podobnego. Chciałem użyć motywu holo tam, gdzie było to możliwe, więc musiałem utworzyć kolejny folder res/values-v11 z plikiem style.xml, w którym ustawiłem parent = "Theme.Holo", który w twoim przypadku byłby "Theme.Holo.Light" . –

+1

Dziękuję bardzo za to! To okropne, ile czasu potrzeba, aby dowiedzieć się, jak sprawić, aby ViewPagerIndicator działał i jak go dostosować ... Gdyby nie StackOverflow i wasi ludzie, sam spróbowałbym popełnić błąd na śmierć. – Terry

5

niewielkim dodatkiem do rozwiązania Michaels: w przypadku korzystania z niestandardowego motywu swojej działalności już, po prostu dodaj theese dwie linie do niego:

<item name="vpiIconPageIndicatorStyle">@style/Widget.IconPageIndicator</item> 
<item name="vpiTabPageIndicatorStyle">@style/Widget.TabPageIndicator</item> 
Powiązane problemy