2017-02-21 110 views
0

嗨我有3個按鈕的活動,我現在已經創建了更改主題應用程序的能力,並且在這裏都可以,但我不知道如何從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)); 
      } 
     } 
    } 
} 

回答

0

您可以創建自定義主題屬性,該屬性將包含的背景參考使用,並將其設置@ drawable/buttonshape for AppTheme.Red@ drawable/buttonshape_blue for AppTheme.Blue

E.g.在你可以有一個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" 
... 
/>