2012-03-29 10 views
18

Próbuję ustawić textize i textcolor z pagertitlestrip i nie mogę znaleźć sposobu, aby to zrobić.Jak mogę ustawić TextColor, TextSize z PagerTitleStrip z kodu?

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

    <android.support.v4.view.PagerTitleStrip 
     android:id="@+id/myStrip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" /> 

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

EDIT

main.xml

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

    <android.support.v4.view.PagerTitleStrip 
     style="@style/PagerTitleStrip" 
     android:id="@+id/myStrip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" /> 

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

style.xml

<style name="PagerTitleStrip"> 
    <item name="android:textAppearance">@style/PagerTitleStripTextAppearance</item> 
</style> 

<style name="PagerTitleStripTextAppearance"> 
    <item name="android:textColor">#FFFFFFFF</item> 
    <item name="android:textSize">14sp</item> 
</style> 

Szkoda, że ​​nie mogę znaleźć sposób, aby go z AttributeSet, uzyskać wyjątek Resource NotFound lub InvalidResource.

+0

Chcesz ustawić tylko te atrybuty tekstu w 'PagerTitleStrip'? – Luksprog

Odpowiedz

2

Look w this

final TypedArray a = context.obtainStyledAttributes(attrs, ATTRS); 
final int textColor = a.getColor(1, 0); 
final int textSize = a.getDimensionPixelSize(2, 0); 
+1

'public PagerTitleStrip (Kontekst kontekstowy, Attrs AttributeSet) { \t super (kontekst, attrs); \t (...) } 'Dowolny przykład utworzenia ** AttributeSet ** dla PagerTitleStrip ?? –

+0

można go utworzyć tylko z xml, spójrz na http://developer.android.com/reference/android/util/AttributeSet.html i http://stackoverflow.com/questions/5316686/what-is-attributeset-and-how-can-i-use-it również pomocne http://grepcode.com/file/repository.grepcode. com/java/ext/com.google.android/android/4.0.3_r1/android/util/AttributeSet.java # AttributeSet –

+0

Napisałem to: 'XmlPullParser parser = getResources(). getXml (R.xml.mypagertitlestripattributes); Atrybuty atrybutów atrybutu = Xml.asAttributeSet (analizator składni); pagerTitleStrip = new PagerTitleStrip (to, atrybuty); pagerTitleStrip = (PagerTitleStrip) findViewById (R.id.myStrip) 'i xml' \t @ zerowy nazwa \t # FFFF0000 \t 30sp ' ale nic się nie zmienia żadnej awarii bez wyjątku –

9

od: http://developer.android.com/reference/android/support/v4/view/PagerTitleStrip.html

ustawiona wartość kolor używany jako kolor bazowy dla wszystkich wyświetlanych tytułów stron:

void setTextColor(int color) 

ustawić domyślny rozmiar tekstu do podana jednostka i wartość:

void setTextSize(int unit, float size) 

wygląda jak nie myślenia.

PagerTitleStrip titleStrip = (PagerTitleStrip) findViewById(R.id.titleStrip); 
titleStrip.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 24); 
+2

Dzięki! Czy można to zrobić także za pomocą XML? – Sheharyar

26

textSize nie jest dostępny bezpośrednio jako własność XML view.PagerTitleStrip.

Jednak udało mi się ustawić go pośrednio za pomocą styl atrybut następująco:

<android.support.v4.view.PagerTitleStrip 
    android:id="@+id/pager_title_strip" 
    style="@style/viewPagerTitleStrip" <-- added style attribute here 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="top" 
    /> 

Potem zdefiniował textSize w styles.xml następująco:

<style name="viewPagerTitleStrip"> 
    <item name="android:layout_width">fill_parent</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:textColor">#FFFFFF</item> 
    <item name="android:textSize">14sp</item> <-- my desired text size 
</style> 

ten pracował dla mnie.

+1

doskonały anwser! – Bernardo

+0

To jest najlepszy sposób! – User3

+1

@ Андрей - To działa. I domaga się "Prawidłowej kontroli" jako właściwej odpowiedzi. Dziękuje wszystkim. – statosdotcom

3

Natknąłem się na to pytanie bardzo późno, ale jak na razie tekst textize, ale nie w opcji przewidywania, działa.

<android.support.v4.view.PagerTitleStrip 
     android:id="@+id/pager_title_strip" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_gravity="top" 
     android:background="@android:color/holo_blue_dark" 
     android:textSize="18sp" 

     android:textColor="#ffffff" 
     android:paddingTop="4dp" 
     android:paddingBottom="4dp" /> 
0

textize, kolor tekstu nie dotyczy tab. Moje kodu jest następujące:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:id="@+id/relativeLayout"> 

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v4.view.PagerTabStrip 
     android:id="@+id/pager_title_strip" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" 
     android:background="#33b5e5" 
     android:textColor="#fff" 
     android:paddingTop="5dp"`enter code here` 
     android:paddingBottom="5dp" /> 

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

Powiązane problemy