2017-08-02 165 views
0

我正在開發應用程序,用戶可以在其中更改THEME(黑暗&白色)。我創造了兩個主題。但我不知道如何讓用戶隨時在應用中更改它。我想不使用開關來實現它。 當我明白我應該改變dinamically風格文件,可能是此行更改Android主題動態

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 

有人能幫助我嗎?因爲我無法找到propper教程

+0

你想整個應用程序的主題改變? –

+0

是的......你說得對 –

回答

0

創建一個定義不同的背景顏色和文字顏色兩個主題:

<resources> 
<style name="BlackTheme" > 

<item name="android:background">#000000</item> 

<item name="android:textColor">#FFFFFF</item> 

</style> 



<style name="BlueTheme" > 

<item name="android:background">#B0E0E6</item> 

<item name="android:textColor">#000000</item> 

</style> 



</resources> 

打開您的佈局文件,輸入如下內容:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

android:layout_width="fill_parent" 

android:layout_height="fill_parent" 

android:orientation="vertical" > 

<TextView 

android:id="@+id/textView1" 

android:layout_width="wrap_content" 

android:layout_height="wrap_content" 

android:layout_alignParentTop="true" 

android:layout_centerHorizontal="true" 

android:layout_marginTop="14dp" 

android:text="@string/pick" 

android:textAppearance="?android:attr/textAppearanceLarge" /> 



<Button 

android:id="@+id/blackbutton" 

android:layout_width="fill_parent" 

android:layout_height="wrap_content" 

android:layout_alignParentLeft="true" 

android:layout_below="@+id/textView1" 

android:text="@string/black" /> 



<Button 

android:id="@+id/bluebutton" 

android:layout_width="fill_parent" 

android:layout_height="wrap_content" 

android:layout_alignRight="@+id/blackbutton" 

android:layout_below="@+id/blackbutton" 

android:text="@string/blue" /> 



<EditText 

android:id="@+id/editText1" 

android:layout_width="wrap_content" 

android:layout_height="wrap_content" 

android:layout_alignLeft="@+id/textView2" 

android:layout_below="@+id/bluebutton" 

android:ems="10" 

android:hint="Name" 

android:inputType="textPersonName" > 



</EditText> 



<TextView 

android:id="@+id/textView2" 

android:layout_width="wrap_content" 

android:layout_height="wrap_content" 

android:layout_alignRight="@+id/editText3" 

android:layout_below="@+id/editText3" 

android:text="@string/agree" /> 



<EditText 

android:id="@+id/editText3" 

android:layout_width="wrap_content" 

android:layout_height="wrap_content" 

android:layout_alignParentLeft="true" 

android:layout_below="@+id/editText1" 

android:ems="10" 

android:hint="Password" 

android:inputType="numberPassword" /> 

</RelativeLayout> 

的strings.xml

<string name="black">Black</string> 

<string name="blue">Blue</string> 

<string name="pick">Pick Your Colour</string> 

<string name="agree">I agree to the terms and conditions</string> 

打開新的文件,並輸入以下內容:

import android.app.Activity; 

import android.content.Intent; 


public class themeUtils 

{ 

private static int cTheme; 



public final static int BLACK = 0; 

public final static int BLUE = 1; 

public static void changeToTheme(Activity activity, int theme) 

{ 

cTheme = theme; 

activity.finish(); 



activity.startActivity(new Intent(activity, activity.getClass())); 


} 

public static void onActivityCreateSetTheme(Activity activity) 

{ 

switch (cTheme) 

{ 

default: 

case BLACK: 

activity.setTheme(R.style.BlackTheme); 

break; 

case BLUE: 

activity.setTheme(R.style.BlueTheme); 

break; 

} 

} 

} 

MainActivity.java

public class MainActivity extends Activity implements OnClickListener 
{ 

/** Called when the activity is first created. */ 

@Override 

public void onCreate(Bundle savedInstanceState) 

{ 

super.onCreate(savedInstanceState); 

themeUtils.onActivityCreateSetTheme(this); 

setContentView(R.layout.activity_main); 

findViewById(R.id.blackbutton).setOnClickListener(this); 

findViewById(R.id.bluebutton).setOnClickListener(this); 

} 

@Override 

public void onClick(View v) 

{ 

switch (v.getId()) 

{ 
case R.id.blackbutton: 

themeUtils.changeToTheme(this, themeUtils.BLACK); 

break; 

case R.id.bluebutton: 

themeUtils.changeToTheme(this, themeUtils.BLUE); 

break; 

} 

} 

} 

看看你的工作。

enter image description hereenter image description here