2017-08-02 1 views
0

私はTHEME(暗い&白)を変更できるアプリを開発しています。私は2つのテーマを作りました。しかし、私はどのようにユーザーがアプリでいつでもそれを変更することができますか分からない。 スイッチを使用して実装したいです。 私はdinamicallyスタイルファイル、おそらくこのラインアンドロイドテーマのダイナミクスを変更してください

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

を変更する必要があります理解できるように誰かが私を助けることができますか?私はpropperのチュートリアルを見つけることができないので、

+0

あなたは変更するアプリケーション全体のテーマにしたいですか? –

+0

はい...あなたは正しいです –

答えて

0

は異なる背景色とテキストの色を定義する2つのテーマを作成します。

<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

関連する問題