gpt4 book ai didi

Android 主题 : Define colors/gradients in "baseTheme.xml", 在控件中使用,在 "subThemeX.xml"中覆盖

转载 作者:可可西里 更新时间:2023-11-01 19:08:17 25 4
gpt4 key购买 nike

我很难弄清楚如何在 android 中实现更复杂的主题/样式情况。

我研究了 Android 提供的不同样式/主题教程,但它们不适合我的情况。

(提炼的)情况如下:我正在创建一个带有自定义选项卡小部件的应用程序,并且我需要能够用不同的样式标记该应用程序 (主题)。

tabwidget 的 XML(基于 http://joshclemm.com/blog/?p=136):

layout/tabs_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/tab_bg_selector"
android:padding="10dip" android:gravity="center" android:orientation="vertical">

<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:orientation="horizontal"
android:gravity="center">
<ImageView
android:src="@drawable/star_fav_empty"
android:layout_height="24px"
android:layout_width="24px"
android:id="@+id/tabsImage"
android:paddingRight="5dip"></ImageView>

<TextView
android:id="@+id/tabsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hallowaaaaa"
android:textSize="15dip"
android:textColor="?android:textColorTertiary"/>
</LinearLayout>
</LinearLayout>

drawable/tab_bg_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item android:state_selected="true" android:state_focused="false"
android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
<!-- Inactive tab -->
<item android:state_selected="false" android:state_focused="false"
android:state_pressed="false" android:drawable="@drawable/tab_bg_unselected" />
<!-- Pressed tab -->
<item android:state_pressed="true" android:state_enabled="false" android:drawable="@android:color/transparent" />
<!-- Selected tab (using d-pad) -->
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="@android:color/transparent" />
</selector>

drawable/tab_bg_selected.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#A8A8A8" android:centerColor="#7F7F7F"
android:endColor="#696969" android:angle="-90" />
</shape>

drawable/tab_bg_unselected.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#5C5C5C" android:centerColor="#424242"
android:endColor="#222222" android:angle="-90" />
</shape>

然后,我想定义样式如下:

values/MyBaseStyle.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyBaseStyle" parent="@android:style/Theme.Light">
</style>
</resources>

值/MySubStyle1.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MySubStyle1" parent="MyBaseStyle">
</style>
</resources>

值/MySubStyle2.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MySubStyle2" parent="MyBaseStyle">
</style>
</resources>

这里的大问题是:

<强>1。如何将渐变或颜色放入 MyBaseStyle.xml,并在 tab_bg_selected.xml 和 tab_bg_unselected.xml 中使用它而不是硬编码渐变/颜色?

<强>2。如何分别从 MySubStyle1.xml 和 MySubStyle2.xml 覆盖我在 MyBaseStyle.xml 中定义的渐变/颜色,以便我的自定义 tabwidget 得到相应的样式?

备注:我真的很想能够分别在 MyBaseStyle.xml、MySubStyle1.xml 和 MySubStyle2.xml 中定义渐变/颜色(而不是在多个不同的 XML 文件中定义多种不同的颜色)以便能够将“样式”保存在一个文件中。这样,我就可以为我的应用程序外包品牌。

有人可以帮我完成这个吗?

最佳答案

在/res/values 的 colors.xml 中为每个主题设置颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="themeB_textColor">#e8e8e8</color>
<color name="themeA_textColor">#fff</color>
</resources>

然后以编程方式为给定主题的 View 设置颜色?

//at on create grab the selected theme however it has been set - ie: through preferences
if(theme=='themeA')
{
super.setTheme(R.style.ThemeA);
Color textColor = getResources().getColor(R.color.themeA_textColor);
}
//later
applyTextColors(textView1,textView2...)

//make a function for applying colors
public void applyTextColors(TextView... tvs)
{
for(TextView tv : tvs){tv.setTextColor(textColor);}
}

关于Android 主题 : Define colors/gradients in "baseTheme.xml", 在控件中使用,在 "subThemeX.xml"中覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6277348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com