将主题更改为按钮

时间:2017-02-21 16:57:32

标签: android xml drawable android-theme

您好我有3个按钮的1个活动,我现在已经创建了更改主题应用程序的功能,并且在这里一切都还可以,但我不知道如何从buttoshape.xml转到buttoshape_blue.xml 这是我的主题:

<resources>

<style name="AppTheme.Blue" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/primaryColor_blue</item>
    <item name="colorPrimaryDark">@color/primaryColorDark_blue</item>
    <item name="colorAccent">@color/primaryAccent_blue</item>
    <item name="backgroundColor">@color/bianco</item>

    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>

</style>

<style name="AppTheme.Red" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/primaryColor_red</item>
    <item name="colorPrimaryDark">@color/primaryColorDark_red</item>
    <item name="colorAccent">@color/primaryAccent_red</item>
    <item name="backgroundColor">@color/bianco</item>


    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>


</style>

我在xml中的按钮

    <Button
    android:id="@+id/magic_item"
    android:text="Magic Item"
    android:textColor="#FFFFFF"
    android:textSize="25dp"

    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@drawable/buttonshape"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    />

这是我的buttonshape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
    android:radius="14dp"
    />
<gradient
    android:angle="45"
    android:centerX="90%"
    android:centerColor="#ec2127"
    android:startColor="#CC1414"
    android:endColor="#FFFFFF"
    android:type="linear"
    />
<padding
    android:left="0dp"
    android:top="0dp"
    android:right="0dp"
    android:bottom="0dp"
    />
<size
    android:width="270dp"
    android:height="60dp"
    />
<stroke
    android:width="3dp"
    android:color="#FFFFFF"
    />

我的实用程序

public class Utility {


    public static void setTheme(Context context, int theme) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs.edit().putInt(context.getString(R.string.prefs_theme_key), theme).apply();
    }
    public static int getTheme(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getInt(context.getString(R.string.prefs_theme_key), -1);
    }
}

和BaseActivity

public class BaseActivity extends AppCompatActivity {

    private final static int THEME_BLUE = 1;
    private final static int THEME_RED = 2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        updateTheme();
    }
    public void updateTheme() {
        if (Utility.getTheme(getApplicationContext()) <= THEME_BLUE) {
            setTheme(R.style.AppTheme_Blue);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                getWindow().setStatusBarColor(getResources().getColor(R.color.primaryColorDark_blue));
            }
        } else if (Utility.getTheme(getApplicationContext()) == THEME_RED) {
            setTheme(R.style.AppTheme_Red);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                getWindow().setStatusBarColor(getResources().getColor(R.color.primaryColorDark_red));
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以创建一个自定义主题属性,其中包含对要使用的背景的引用,并将其设置为 @ drawable / buttonshape ,用于 AppTheme.Red @ drawable / buttonshape_blue for AppTheme.Blue

E.g。在 values 中你可以有一个 attrs.xml 文件,其中包含:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="buttonBackground" format="reference" />
</resources>

并在你的themes.xml中:

<style name="AppTheme.Blue" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/primaryColor_blue</item>
    <item name="colorPrimaryDark">@color/primaryColorDark_blue</item>
    <item name="colorAccent">@color/primaryAccent_blue</item>
    <item name="backgroundColor">@color/bianco</item>

    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="backgroundColor">@color/bianco</item>
    <item name="buttonBackground">@drawable/buttonshape_blue</item>

</style>

<style name="AppTheme.Red" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/primaryColor_red</item>
    <item name="colorPrimaryDark">@color/primaryColorDark_red</item>
    <item name="colorAccent">@color/primaryAccent_red</item>
    <item name="backgroundColor">@color/bianco</item>

    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="buttonBackground">@drawable/buttonshape</item>

</style>

布局文件中的按钮如下所示:

<Button
android:id="@+id/magic_item"
...
android:background="?attr/buttonBackground"
...
/>