android– 仅将样式应用于特定的TextView

android– 仅将样式应用于特定的TextView,第1张

概述我有两个主题,我在Android应用程序之间来回切换.在某些地方,我有TextViews,我想保持默认颜色.在其他地方,我希望它们是另一种颜色(让我们说橙色).在更改主题时,我只希望之前为橙色的TextView变为蓝色.有没有办法在Android中执行此 *** 作?我有一些想法,比如html/css中的类标记,但似

我有两个主题,我在Android应用程序之间来回切换.在某些地方,我有TextVIEws,我想保持默认颜色.在其他地方,我希望它们是另一种颜色(让我们说橙色).在更改主题时,我只希望之前为橙色的TextVIEw变为蓝色.

有没有办法在AndroID中执行此 *** 作?我有一些想法,比如HTML / CSS中的类标记,但似乎找不到任何东西.

编辑:用HTML / CSS等价来澄清我所说的内容:

<div>This text is black</div><div >This text is red</div>.reddiv {    color:red;}

解决方法:

假设您的应用程序中有两个主题: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>

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>

因此,当你有一个带有两个TextVIEw的布局时,你可以通过不设置任何额外的东西来完全跟随活动主题,并且可以通过为其指定样式属性将其他外观应用于其他TextVIEw实例:

<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"                androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:layout_below="@ID/tv_styledependent" /></relativeLayout>
总结

以上是内存溢出为你收集整理的android – 仅将样式应用于特定的TextView全部内容,希望文章能够帮你解决android – 仅将样式应用于特定的TextView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1110567.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存