Chr*_*her 9 android textview android-appcompat
我想在我的AppTheme中设置一个默认的textcolor,它应该是黑色的(不是默认的Material Design深灰色).应该通过在UI元素(例如TextView)上通过android:textAppearance-attribute设置自定义样式来覆盖textColor.
这是我目前的设置:
我在我的项目中使用AppCompat-Library:
compile 'com.android.support:appcompat-v7:22.2.0'
Run Code Online (Sandbox Code Playgroud)
我定义了以下主题:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
<item name="android:textColorPrimary">@color/black</item> <!-- All TextViews should have this color as default text color -->
</style>
Run Code Online (Sandbox Code Playgroud)
在AndroidManifest.xml中设置:
<application
android:name=".core.BaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
Run Code Online (Sandbox Code Playgroud)
此外,我定义了一些样式来改变一些TextViews的textAppearance:
<style name="App.Heading" parent="android:TextAppearance.Widget.TextView">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/red</item>
</style>
<style name="App.Heading.Highlighted" parent="android:TextAppearance.Widget.TextView">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/blue</item>
</style>
Run Code Online (Sandbox Code Playgroud)
现在我有一些带有一些TextView的xml布局.其中一些应该具有默认的textAppearance(实际上是在我的主题中定义为android:textColorPrimary的黑色文本颜色).有些应该应用我定义的样式的自定义textappearance:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="CUSTOM: App.Heading"
android:textAppearance="@style/App.Heading" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="CUSTOM: App.Heading.Highlighted"
android:textAppearance="@style/App.Heading.Highlighted" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Normal Textview with no style"/>
Run Code Online (Sandbox Code Playgroud)
前两个TextView应用我定义的textAppearance,这很好.但是最后一个TextView有一个深灰色的文本颜色(Material Design默认?)而不是黑色的.如果我设置属性:
<item name="android:textColor">@color/black</item>
Run Code Online (Sandbox Code Playgroud)
在我的AppTheme中,所有TextView都有黑色文本颜色.我的样式中定义的textcolor(例如App.Heading)不再被识别.
所以我的问题是:如何为TextView设置默认的textColor,可以通过设置textAppearance来覆盖它?
GPa*_*ack 16
找到方法并不容易,因为有三个可设置的android:textColor属性,textColorPrimary textColorSecondary和textColorTertiary,以及基本样式的许多属性android:textAppearance作为小介质等(现在已弃用?).
无论如何,关于没有样式的TextView,它似乎默认为android:textAppearanceSmallstyleable属性,其值是TextAppearance.AppCompat.Small通过带有值Base.TextAppearance.AppCompat.Small的android:textColor属性覆盖的样式?android:attr/textColorTertiary.
然后像下面那样覆盖它将起作用:
<item name="android:textColorTertiary">@color/black</item>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8547 次 |
| 最近记录: |