向主题添加不同样式的视图并在运行时更改它?
我用样式设置了特定 TextView 和其他内容的样式。现在我想实现另一个主题,它应该特别更改 styles.xml
中定义的颜色。那么是否可以将所有这些样式添加到主题中并在运行时使用 act.setTheme(themeID);
更改它?
以下是一些代码示例:
ListView Layout.xml(看起来有点混乱,但样式属性应该很容易找到。)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/OrangeTheme_ListView_Item"
android:id="@+id/MainListItemBG" android:padding="10dp"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_width="fill_parent">
<TextView
style="@style/OrangeTheme_ListView_Item_TextHead"
android:text="TextView" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/MainList_Head" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TableLayout android:stretchColumns ="*" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/tableLayout1">
<TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
style="@style/OrangeTheme_ListView_Item_TextBody"
android:layout_width="fill_parent" android:text="test"
android:layout_height="fill_parent" android:id="@+id/MainList_Body_Left"></TextView>
<TextView
style="@style/OrangeTheme_ListView_Item_TextBody" android:layout_width="fill_parent" android:text="test" android:layout_height="fill_parent" android:id="@+id/MainList_Body_Right"></TextView>
</TableRow>
</TableLayout>
</LinearLayout>
我的 styles.xml 的一部分
<style name="OrangeTheme_ListView">
<item name="android:background">#FFFFFF</item>
<item name="android:layout_marginRight">5dp</item>
<item name="android:layout_marginLeft">5dp</item>
<item name="android:divider">#FFFFFF</item>
<item name="android:dividerHeight">5dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:cacheColorHint">#00000000</item>
</style>
<style name="OrangeTheme_ListView_Item">
<item name="android:background">@drawable/orange_theme_lv_bg</item>
<item name="android:textColor">#000000</item>
<item name="android:layout_marginRight">10dp</item>
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:layout_marginTop">10dp</item>
</style>
<style name="OrangeTheme_ListView_Item_TextHead">
<item name="android:textColor">#000000</item>
</style>
<style name="OrangeTheme_ListView_Item_TextBody">
<item name="android:textColor">#000000</item>
<item name="android:textSize">12sp</item>
</style>
I styled specific TextViews and other stuff with styles. Now I want to implement another theme, which should change espacially the colors which are defined in styles.xml
. So is it possible to add all of those styles to a theme and change it on runtime maybe with act.setTheme(themeID);
?
Here are some code samples:
ListView Layout.xml (Looks a little bit chaotic but the style attributes should be easy to find.)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/OrangeTheme_ListView_Item"
android:id="@+id/MainListItemBG" android:padding="10dp"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_width="fill_parent">
<TextView
style="@style/OrangeTheme_ListView_Item_TextHead"
android:text="TextView" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/MainList_Head" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TableLayout android:stretchColumns ="*" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/tableLayout1">
<TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
style="@style/OrangeTheme_ListView_Item_TextBody"
android:layout_width="fill_parent" android:text="test"
android:layout_height="fill_parent" android:id="@+id/MainList_Body_Left"></TextView>
<TextView
style="@style/OrangeTheme_ListView_Item_TextBody" android:layout_width="fill_parent" android:text="test" android:layout_height="fill_parent" android:id="@+id/MainList_Body_Right"></TextView>
</TableRow>
</TableLayout>
</LinearLayout>
A part of my styles.xml
<style name="OrangeTheme_ListView">
<item name="android:background">#FFFFFF</item>
<item name="android:layout_marginRight">5dp</item>
<item name="android:layout_marginLeft">5dp</item>
<item name="android:divider">#FFFFFF</item>
<item name="android:dividerHeight">5dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:cacheColorHint">#00000000</item>
</style>
<style name="OrangeTheme_ListView_Item">
<item name="android:background">@drawable/orange_theme_lv_bg</item>
<item name="android:textColor">#000000</item>
<item name="android:layout_marginRight">10dp</item>
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:layout_marginTop">10dp</item>
</style>
<style name="OrangeTheme_ListView_Item_TextHead">
<item name="android:textColor">#000000</item>
</style>
<style name="OrangeTheme_ListView_Item_TextBody">
<item name="android:textColor">#000000</item>
<item name="android:textSize">12sp</item>
</style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,您还无法在运行时更改样式。 此处
是解释,为什么。您可以做的是创建不同的布局并根据您的应用程序所处的情况加载它们。
Unfortunately you can't change styles at runtime, yet. Here
is the explanation, why. What you can do is to create different layouts and load them according to the situation your App is in.