应用品牌

时间:2012-09-27 09:12:39

标签: android android-theme

我希望我的应用程序可以在不同的品牌下使用 - 这意味着必须能够在很少的地方轻松更改背景位图,还可以更改文本资源。

我的计划是使用主题和样式,我知道它们可用于切换位图但是它们是否允许我更改TextViews中的文本?

是否也可以在样式或主题中指定文本标识符,然后在代码中动态读取它?

[编辑]

经过一些调查,当然文本可以用样式代替。应用程序品牌化带来的另一个问题是需要更改软件包名称 - 否则Google Play将不会接受我们的品牌应用程序。

[编辑]

经过更多调查,下面我将介绍如何添加两个可切换主题的小样本,这些主题允许在活动布局中使用可替换的可绘制主题,以及在textview中添加文本资源。剩下的就是打电话给setTheme(R.style.Theme1);或setTheme(R.style.Theme2);在setContentView之前的onCreate中。

<-- 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>

1 个答案:

答案 0 :(得分:0)

有两种方法可以为您制作不同的品牌:

1)将您的应用程序拆分为多个库项目,常见的代码就是使用common_lib库项目,所有品牌都在使用common_lib的单独项目中,但更改了一些资源,如字符串,一些drawable,以及包名。这是我们在我们的应用程序中选择的方法。

2)使用gradle产品口味:http://developer.android.com/sdk/installing/studio-build.html,我认为现在应该是首选解决方案。

相关问题