我有两个主题,我在Android应用程序之间来回切换.在某些地方,我TextView想要保持默认颜色.在其他地方,我希望它们是另一种颜色(让我们说橙色).在改变主题时,我只想要TextView以前变成蓝色的s.
有没有办法在Android中执行此操作?我有一些想法,比如html/css中的类标记,但似乎找不到任何东西.
编辑:用html/css等价来澄清我所说的内容:
<div>This text is black</div>
<div class="redDiv">This text is red</div>
.redDiv {
color:red;
}
Run Code Online (Sandbox Code Playgroud)
假设您的应用程序中有两个主题:MyFirstTheme和MySecondTheme:
<style name="MyFirstTheme" parent="android:Theme">
<item name="android:textViewStyle">@style/MyFirstTheme.TextView</item>
[...]
</style>
<style name="MySecondTheme" parent="android:Theme">
<item name="android:textViewStyle">@style/MySecondTheme.TextView</item>
[...]
</style>
Run Code Online (Sandbox Code Playgroud)
styles.xml中定义的textview样式可能如下所示:
<style name="MyFirstTheme.TextView" parent="@android:style/Widget.TextView">
<item name="android:textColor">@android:color/black</item>
[...]
</style>
<style name="MySecondTheme.TextView" parent="@android:style/Widget.TextView">
<item name="android:textColor">@color/orange</item>
[...]
</style>
<style name="PersistentTheme.TextView" parent="@style/MyFirstTheme.TextView">
<item name="android:textColor">@color/red</item>
</style>
Run Code Online (Sandbox Code Playgroud)
所以当你有一个带有两个TextView的布局时,你可以通过不设置任何额外的东西来完全跟随活动主题,并且你可以通过为其他TextView实例指定一个style属性来应用其他外观:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- This textview will change text color when other style applied -->
<TextView
android:id="@+id/tv_styledependent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- This textview will always have the same (red) text color -->
<TextView
android:id="@+id/tv_persistent"
style="@style/PersistentTheme.TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_styledependent" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2490 次 |
| 最近记录: |