我希望我的应用程序在不同的品牌下可用-这意味着它必须能够轻松地在几个地方更改背景位图,但也可以更改文本资源。
我的计划是使用主题和样式,我知道它们可以用来切换位图,但是它们允许我在TextViews中也改变文本吗?
还可以在样式或主题中指定文本标识符,然后在代码中动态读取它吗?
编辑
经过一些调查之后,当然可以用样式来代替文本。应用程序品牌化的另一个问题是需要更改包名称-否则Google Play不会接受我们的品牌化应用程序。
编辑
经过更多的调查,下面我包括了一个小样本,关于如何添加两个可切换的主题,将允许替代活动布局中的可绘制,以及文本视图中的文本资源。剩下的就是在onCreate中调用setTheme(R.style.Theme1);或setTheme(R.style.Theme2);,然后再调用setContentView。
<-- attrs.xml />
<resources>
<attr name="ProductName" format="reference"/>
<attr name="ProductBackground" format="integer"/>
</resources>
<-- styles.xml />
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme1" parent="android:Theme.Light">
<item name="ProductName">@string/s_product_1_name</item>
<item name="ProductBackground">@drawable/back_1_vga</item>
</style>
<style name="Theme2" parent="android:Theme.Light" >
<item name="ProductName">@string/s_product_2_name</item>
<item name="ProductBackground">@drawable/back_2_vga</item>
</style>
</resources>
<-- my activity layout />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?ProductBackground"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
tools:context=".MainActivity"
android:background="#ff0000"
android:text="?ProductName"
/>
</RelativeLayout>发布于 2014-04-05 16:57:28
有两种方法可以让你的应用程序有不同的品牌:
1)将你的应用程序分成多个库项目,公共代码在common_lib库项目中,所有的品牌都在使用common_lib的单独项目中,但更改了一些资源,如字符串,一些绘图文件,也包括包名。这是我们在应用程序中选择的方法。
2)使用gradle产品风格:http://developer.android.com/sdk/installing/studio-build.html,我认为这应该是现在更好的解决方案。
https://stackoverflow.com/questions/12617910
复制相似问题