当主题从 Theme.AppCompat.Light.DarkActionBar 扩展时,如何在整个应用程序中设置 TextView 和编辑文本的样式?
这是行不通的,
<style name="myTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:textViewStyle">@style/myTextViewStyle</item>
<item name="textViewStyle">@style/myTextViewStyle</item>
</style>
<style name="myTextViewStyle" parent="android:Widget.TextView">
<item name="android:layout_marginLeft">5dp</item>
<item name="android:layout_marginRight">5dp</item>
<item name="android:layout_marginTop">5dp</item>
<item name="android:layout_marginBottom">5dp</item>
<item name="android:textColor">@android:color/black</item>
</style>
最佳答案
<!-- Maintain *theme* naming convention Theme.ThemeName. It helps with organization. -->
<style name="Theme.MyApp" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:textViewStyle">@style/Widget.MyApp.TextView</item>
<!-- AppCompat does not provide its own variant of textViewStyle attribute. -->
<item name="editTextStyle">@style/Widget.MyApp.EditText</item>
</style>
<!-- Maintain *style* naming convention Widget.ThemeName.WidgetName. -->
<!-- TextView is a simple widget, use the parent provided by platform. -->
<style name="Widget.MyApp.TextView" parent="android:Widget.TextView">
<!-- ... -->
</style>
<!-- EditText parent is provided by AppCompat. -->
<style name="Widget.MyApp.EditText" parent="Widget.AppCompat.EditText">
<!-- ... -->
</style>
只要您的应用或 Activity 在 list 中定义了 android:theme="@style/Theme.MyApp
,它就可以工作。
注意事项:
- 如果您想为另一个小部件提供默认样式,请尝试这样做:
- 在您的主题中覆盖
widgetNameStyle
。 - 如果该属性不存在,请在其前面加上
android:
。 - 如果这不起作用,那么它就是一个自定义小部件,这超出了本文的范围。
- 不要同时覆盖
android:
前缀和无前缀的变体(如果存在)!只有一个有效。 - 无前缀属性来自 AppCompat,使用 AppCompat 样式作为父级。
- 前缀属性来自 Android SDK,使用平台样式作为父级(
TextView
和ProgressBar
)。
- 在您的主题中覆盖
- 不要混合使用主题和样式。主题在上下文范围内应用于整个 View 层次结构(
android:theme
属性)。样式用于小部件(style
属性)。
关于android - 如何使用 appcompat 设置 View 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24065402/