【问题标题】:Extend appcompat textview style扩展 appcompat textview 样式
【发布时间】:2017-02-15 20:18:01
【问题描述】:

我需要为我的应用程序中的所有 TextView/Button 设置自定义样式(我需要更改字体系列)

为了实现我的意图,我在我的 App Style 中编写了以下代码:

<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>

如您所见,我正在使用 AppCompat 主题,但我不知道如何扩展正确的 textview。事实上,我正在使用parent="android:Widget.Holo.Button",但这样我就失去了 AppCompat 按钮样式。

PS:使用该代码,我的所有文本视图和按钮都改变了字体系列,除了工具栏和 NavigationView,这正常吗?

【问题讨论】:

    标签: android android-appcompat


    【解决方案1】:
    <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>
    

    【讨论】:

    • 谢谢,但我需要一个 TextView 而不是 AutocompleteTextView。使用 Widget.AppCompat.textView 我收到错误“找不到资源”
    • 试试 Widget.AppCompat.TextView.SpinnerItem 或 Widget.TextView
    • 试试 TextAppearance.AppCompat
    • @style/TextAppearance.AppCompat .Not textViewStyle
    • 我去睡觉了。再见。
    【解决方案2】:

    步骤 1) 将 .ttf 格式(Roboto-Black.ttf、Roboto-Bold.ttf、RobotoCondensed-Bold.ttf、Roboto-Regular.ttf 等)中急需的字体文件的变体复制到“assets”目录。(创建如果您在“主”目录中还没有一个,则一个)。

    步骤 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>
    

    最终在 Layout 文件中使用它们

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      相关资源
      最近更新 更多