使用一个主题的两个不同颜色的文本视图?
Two textviews with different colors using one theme?
我是 android 的新手,我一直在努力寻找这个问题的答案。我有一个布局文件 .xml for listview
,它有两个文本视图,一个用于更大尺寸的标题,另一个用于描述。我想要的是标题 textview 的文本在我当前的主题中保持白色,描述 textview 的文本保持灰色。那是黑暗的主题。但是用户可以选择更改主题,因此当用户选择 Light 主题时,我希望标题文本视图的文本(在列表视图行的布局内)变为黑色,并且描述文本视图的文本也变为黑色。
请帮助并提前致谢。
您可以使用方法 textView.setTextColor(int color) 以编程方式设置文本颜色,或者通过 XML 使用属性 android:textColor="#yourhexcolor"
您可以通过编程方式更改主题。几个小时前有人问过这个问题。你能检查一下吗:how-to-change-colour-when-user-wants-another-colour-in-the-app
此外,如果您想为不同的 TextViews
使用不同的主题,您可以尝试:
textView.setTextAppearance(context, android.R.style.TextAppearance_Small);
How can i create custom themes for TextViews
在您的 styles.xml
中创建不同的样式:
<style name="MyBlueTextTheme" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#123456</item>
<item name="android:textStyle">bold</item>
</style>
然后像这样使用它:
<TextView
android:id="@+id/textBlue"
android:text="This is a blue styled text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
style="@style/MyBlueTextTheme" />
或像这样使用:textView.setTextAppearance(context,R.style.MyBlueTextTheme);
但它已被弃用,您可以使用 textView.setTextAppearance(R.style.MyBlueTextTheme);
但这可以使用 Api23
或更高
我是 android 的新手,我一直在努力寻找这个问题的答案。我有一个布局文件 .xml for listview
,它有两个文本视图,一个用于更大尺寸的标题,另一个用于描述。我想要的是标题 textview 的文本在我当前的主题中保持白色,描述 textview 的文本保持灰色。那是黑暗的主题。但是用户可以选择更改主题,因此当用户选择 Light 主题时,我希望标题文本视图的文本(在列表视图行的布局内)变为黑色,并且描述文本视图的文本也变为黑色。
请帮助并提前致谢。
您可以使用方法 textView.setTextColor(int color) 以编程方式设置文本颜色,或者通过 XML 使用属性 android:textColor="#yourhexcolor"
您可以通过编程方式更改主题。几个小时前有人问过这个问题。你能检查一下吗:how-to-change-colour-when-user-wants-another-colour-in-the-app
此外,如果您想为不同的 TextViews
使用不同的主题,您可以尝试:
textView.setTextAppearance(context, android.R.style.TextAppearance_Small);
How can i create custom themes for TextViews
在您的 styles.xml
中创建不同的样式:
<style name="MyBlueTextTheme" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#123456</item>
<item name="android:textStyle">bold</item>
</style>
然后像这样使用它:
<TextView
android:id="@+id/textBlue"
android:text="This is a blue styled text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
style="@style/MyBlueTextTheme" />
或像这样使用:textView.setTextAppearance(context,R.style.MyBlueTextTheme);
但它已被弃用,您可以使用 textView.setTextAppearance(R.style.MyBlueTextTheme);
但这可以使用 Api23
或更高