2012-11-09 30 views
17

Tôi đang sử dụng PagerTabStrip trong ViewPager để hiển thị tiêu đề của mỗi trang. Đây là mã XML của tôi:Làm thế nào để thiết lập TextStyle của PagerTabStrip sang Bold?

<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:background="@drawable/bg"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    > 
    <android.support.v4.view.PagerTabStrip 
      android:id="@+id/pager_title_strip" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="top" 
      android:background="#33b5e5" 
      android:textColor="#a4c639" 
      android:paddingTop="4dp" 
      android:paddingBottom="4dp" /> 
    </android.support.v4.view.ViewPager> 

</RelativeLayout> 

tôi muốn thiết lập các textStyle để in đậm trong PagerTabStrip như thế này: android:textStyle="bold". Nhưng điều này là không thể trong PagerTabStrip. Có vẻ như nó chỉ có thuộc tính textcolor cho văn bản. Cách mà tôi có thể thiết lập kiểu in đậm là gì?

+0

Bạn có gì trong '@ style/TitleStripTextAppearance'? – CommonsWare

+0

@CommonsWare Oh Tôi quên xóa dòng này tại thời điểm đăng câu hỏi. Tôi đã cố gắng để thêm phong cách trong '@ style/TitleStripTextAppearance' nhưng tôi không chắc chắn sẽ làm việc này hay không? – Jaguar

Trả lời

40

Những gì bạn cần làm là tạo ra một phong cách cho các văn bản, và sau đó sử dụng android: attribute textAppearance ...

Có lẽ một cái gì đó như thế này:

<style name="PagerTabStripText"> 
     <item name="android:textSize">14sp</item> 
     <item name="android:textStyle">bold</item> 
     <item name="android:textColor">#a4c639</item> 
</style> 

Và sau đó trong XML PagerTabStrip của bạn làm điều này:

<android.support.v4.view.PagerTabStrip 
     android:id="@+id/pager_title_strip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" 
     android:background="#33b5e5" 
     android:paddingTop="4dp" 
     android:paddingBottom="4dp" 
     android:textAppearance="@style/PagerTabStripText"/> 
+11

Nhân tiện, eclipse doesnt autosuggest android: textAppearance bên trong android.support.v4.view.PagerTabStrip. Nhưng đừng nhầm lẫn với điều đó, textAppearance hoạt động cho PagerTabStrip. –

+1

Tôi cần sử dụng kiểu chữ tùy chỉnh cho pagertabstrip của mình. Bất kỳ ý tưởng làm thế nào để sử dụng? – ambit

+0

@Justin - 'textMultiLine' tài sản không hoạt động theo cách này. Có cách nào khác không? – Braj

4

PagerTabStrip không aceppts textỨng dụng.

Giống như @Justin nói ở trên, nhưng trên xml đặt:

style="@style/PagerTabStripText"

không:

android:textAppearance="@style/PagerTabStripText"

2

Nếu bạn muốn tạo kiểu cho tiêu đề tab trung tâm khác nhau từ những người khác , sau đó bạn sẽ cần phải sử dụng sự phản chiếu:

try { 
    final Class pagerTitleStripClass = PagerTitleStrip.class; 
    final Field mCurrTextField = pagerTitleStripClass.getDeclaredField("mCurrText"); 
    mCurrTextField.setAccessible(true); 

    // mTitle is my class's PagerTabStrip member variable 
    final TextView mCurrText = (TextView) mCurrTextField.get(mTitle); 
    mCurrText.setTypeface(Typeface.DEFAULT_BOLD); 
} catch (final Exception e) { 
    Log.w(TAG, "Exception when styling currently selected title!", e); 
} 

Thật không may, giải pháp này có thể không hoạt động nữa khi cập nhật cho các Android Support Library được phát hành (vì đó là nơi mà các lớp PagerTitleStrip là từ). Cụ thể, mẫu mã này đang hoạt động với revision 20.

+0

Cá nhân tôi sẽ không khuyên bạn sử dụng sự phản chiếu cho các loại "thủ thuật" này. Có một lý do nó là riêng tư - Nó có thể thay đổi bất cứ lúc nào - ngay cả trong các cam kết khác nhau của cùng một sửa đổi. Phản ánh nên được sử dụng cho các công cụ siêu cấp như viết khung và như vậy. – kamathln

Các vấn đề liên quan