2016-02-24 16 views
5

Tôi đang sử dụng TabLayout từ thư viện Hỗ trợ thiết kế Android và muốn tạo kiểu văn bản (tiêu đề). Cụ thể làm cho nó đậm. Làm thế nào để đạt được điều đó trong XML chỉ?Làm văn bản TabLayout in đậm

<android.support.design.widget.TabLayout 
android:id="@+id/sliding_tabs" 
android:layout_width="match_parent" 
app:tabTextColor="@color/white" 
app:tabSelectedTextColor="@color/white" 
app:tabIndicatorColor="@color/accent" 
android:layout_height="wrap_content" 
app:tabIndicatorHeight="3dp" /> 

Trả lời

15

đầu tiên này phải được bổ sung vào styles.xml:

<style name="TabLayoutTextStyle"> 
    <item name="android:textSize">16sp</item> 
    <item name="android:textStyle">bold</item> 
</style> 

Thậm chí nếu bạn không muốn thay đổi kích thước văn bản, bạn phải bao gồm nó trong phong cách nếu không sẽ không có gì được hiển thị.

Sau đó, kiểu phải được áp dụng cho TabLayout bằng cách sử dụng app:tabTextAppearance không phải thuộc tính style!

<android.support.design.widget.TabLayout 
android:id="@+id/sliding_tabs" 
android:layout_width="match_parent" 
app:tabTextColor="@color/white" 
app:tabSelectedTextColor="@color/white" 
app:tabIndicatorColor="@color/accent" 
android:layout_height="wrap_content" 
app:tabIndicatorHeight="3dp" 
app:tabTextAppearance="@style/TabLayoutTextStyle" /> 

Để kích hoạt allcaps bạn có thể thêm dòng sau vào các TabLayoutTextStyle:

<item name="android:textAllCaps">true</item> 
+5

nó bị lỗi, hoạt động khi tôi cũng khai báo kiểu textColor. – Darpan

0

Thêm TabLayout Text Style trong styles.xml


<style name="TabLayoutTextStyle"> 
    <item name="android:textStyle">bold</item> 
</style> 

Và thiết TabLayoutTextStyle như phong cách để Yor tính TabLayout.


<android.support.design.widget.TabLayout 
android:id="@+id/sliding_tabs" 
android:layout_width="match_parent" 
app:tabTextColor="@color/white" 
app:tabSelectedTextColor="@color/white" 
app:tabIndicatorColor="@color/accent" 
android:layout_height="wrap_content" 
app:tabIndicatorHeight="3dp" 
style="@style/TabLayoutTextStyle" /> 
+0

Không hoạt động ... Tôi đã tìm thấy giải pháp. – VSG24

+0

Làm theo liên kết này, Có lẽ nó sẽ hoạt động ... [Văn bản tạo kiểu trên TabLayout] (http://stackoverflow.com/a/31271945/4910513) –

0
  1. Một lựa chọn là để thêm Trong styles.xml

    <item name="android:textStyle">bold</item> 
    

    bên trong "TextAppearance. Design.Tab "có cùng tên với cha mẹ

    <style name="TextAppearance.Design.Tab" parent="TextAppearance.Design.Tab"> 
        <item name="android:textSize">15sp</item> 
        <item name="android:textStyle">bold</item> 
        <item name="android:textColor">?android:textColorSecondary</item> 
        <item name="textAllCaps">true</item> 
        <item name="android:singleLine">true</item> 
    </style> 
    
  2. tùy chọn khác: bên trong bố trí của bạn hướng đến phong cách của bạn - cho phép nói rằng bạn gọi nó là myTabLayoutStyle

    style="@style/myTabLayoutStyle" 
    

và bên trong phong cách mà chuyển hướng một lần nữa để phong cách khác chỉ dành riêng cho diện mạo văn bản:

 <item name="tabTextAppearance">@style/myTabTextStyle</item> 

như sau:

<android.support.design.widget.TabLayout 
    android:id="@+id/tab_layout" 
    style="@style/myTabLayoutStyle" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/toolbar" 
    android:background="?attr/colorPrimary" 
    android:elevation="600dp" 
    android:minHeight="?attr/actionBarSize" 
    app:tabGravity="fill" 
    android:singleLine="true" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 

bên trong styles.xml:

<style name="myTabLayoutStyle" parent="Widget.Design.TabLayout"> 
    <item name="tabMaxWidth">@dimen/tab_max_width</item> 
    <item name="tabIndicatorColor">?attr/colorAccent</item> 
    <item name="tabIndicatorHeight">4dp</item> 
    <item name="tabPaddingStart">3dp</item> 
    <item name="tabPaddingEnd">3dp</item> 
    <item name="android:singleLine">true</item> 
    <item name="tabBackground">?attr/selectableItemBackground</item> 
    <item name="tabSelectedTextColor">?android:textColorPrimary</item> 
    <item name="tabTextAppearance">@style/myTabTextStyle</item> 
    </style> 

    <style name="myTabTextStyle"> 
     <item name="android:textSize">15sp</item> 
     <item name="android:textStyle">bold</item> 
     <item name="android:textColor">?android:textColorSecondary</item> 
     <item name="textAllCaps">true</item> 
     <item name="android:singleLine">true</item> 
    </style> 
Các vấn đề liên quan