2015-11-05 85 views
3

我需要設置一個自定義樣式到所有的TextView /按鈕在我的應用程序(我需要改變FONT-FAMILY)擴展程序兼容性的TextView風格

爲了實現我的意圖,我寫我的應用程序樣式下面的代碼:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
    <item name="android:windowBackground">@null</item> 
    <item name="android:textColor">@color/textColorPrimary</item>= 
    <item name="android:textViewStyle">@style/RobotoTextViewStyle</item> 
    <item name="buttonStyle">@style/RobotoButtonStyle</item> 

    <!-- <item name="android:typeface">monospace</item> --> 
</style> 

<style name="RobotoTextViewStyle" parent="android:Widget.TextView"> 
    <item name="android:fontFamily">sans-serif-condensed-light</item> 
</style> 

<style name="RobotoButtonStyle" parent="android:Widget.Holo.Button"> 
    <item name="android:fontFamily">sans-serif-condensed-light</item> 
</style> 

正如你可以看到我使用的程序兼容性的主題,但我不能弄清楚如何擴展TextView的正確。 Infact我正在使用parent="android:Widget.Holo.Button"但以這種方式我失去了AppCompat按鈕樣式。

PS:使用該代碼,我的所有文本視圖和按鈕都會更改字體系列,除了工具欄和NavigationView之外,是否正常?

回答

0
<style name="RobotoTextViewStyle" parent="Widget.AppCompat.AutoCompleteTextView"> 
    <item name="android:fontFamily">sans-serif-condensed-light</item> 
</style> 

<style name="RobotoButtonStyle" parent="Widget.AppCompat.Button"> 
    <item name="android:fontFamily">sans-serif-condensed-light</item> 
</style> 
+0

由於使用它們,但我需要一個TextView不是AutocompleteTextView。使用Widget.AppCompat.textView我得到一個錯誤「找不到資源」 – user3471528

+0

嘗試Widget.AppCompat.TextView.SpinnerItem或Widget.TextView –

+0

嘗試TextAppearance.AppCompat –

0

步驟1) 複印過的非常需要的字體文件在.TTF格式(的Roboto-Black.ttf,的Roboto-Bold.ttf,RobotoCondensed-Bold.ttf,的Roboto-Regular.ttf等變化..)到'資產'目錄(如果你還沒有在'main'目錄下,創建一個)。

步驟2) 在'xml'文件夾中創建一個名爲'fonts.xml'的文件,該文件應位於'res'目錄中。 (RES \ XML \ fonts.xml)。聲明您的首選字體名稱以匹配來自'資產'目錄的匹配文件名稱。

<!-- Font with 10 styles defined --> 
<family> 
    <nameset> 
     <name>rb_regular</name> 
     <name>rb_bold</name> 
     <name>rb_light</name> 
     <name>rb_bolditalic</name> 
     <name>rb_thin</name> 
     <name>rb_medium</name> 
     <name>rb_condensed_regular</name> 
     <name>rb_condensed_light</name> 
     <name>rb_condensed_bold</name> 
     <name>rb_black</name> 
    </nameset> 
    <fileset> 
     <file>Roboto-Regular.ttf</file> 
     <file>Roboto-Bold.ttf</file> 
     <file>Roboto-Light.ttf</file> 
     <file>Roboto-BoldItalic.ttf</file> 
     <file>Roboto-Thin.ttf</file> 
     <file>Roboto-Medium.ttf</file> 
     <file>RobotoCondensed-Regular.ttf</file> 
     <file>RobotoCondensed-Light.ttf</file> 
     <file>RobotoCondensed-Bold.ttf</file> 
     <file>Roboto-Black.ttf</file> 
    </fileset> 
</family> 

在樣式文件中定義它們,如下

//textviews 
<style name="TextViewRobotoRegular" parent="android:Widget.TextView"> 
    <item name="android:fontFamily" tools:targetApi="jelly_bean">rb_regular</item> 
</style> 
<!-- Roboto-Bold--> 
<style name="TextViewRobotoBold" parent="android:Widget.TextView"> 
    <item name="android:fontFamily" tools:targetApi="jelly_bean">rb_bold</item> 
</style> 
<!-- Roboto-Light--> 
<style name="TextViewRobotoLight" parent="android:Widget.TextView"> 
    <item name="android:fontFamily" tools:targetApi="jelly_bean">rb_italic</item> 
</style> 
<!-- Roboto-BoldItalic--> 
<style name="TextViewRobotoBoldItalic" parent="android:Widget.TextView"> 
    <item name="android:fontFamily" tools:targetApi="jelly_bean">rb_bolditalic</item> 

</style> 

//button 
<style name="RobotoRegular" parent="android:Widget.Button"> 
    <item name="android:fontFamily" tools:targetApi="jelly_bean">rb_regular</item> 
</style> 

終於在佈局文件

     <TextView 
         style="@style/TextViewRobotoLight" 
         android:layout_width="75dp" 
         android:layout_height="30dp" />