更改所有布局背景颜色取决于按钮的值

时间:2016-09-03 12:51:27

标签: java android button

我尝试进行一项活动,用户可以从按钮中选择颜色,点击其中一个后,整个应用程序的布局会根据按钮的值进行更改。对不起,我刚刚开始一周的android编程,noob在这里。 这是我在theme_layout

中的代码
<?xml version="1.0" encoding="utf-8"?>

`<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="#618685"
android:id="@+id/relative"
tools:Context="com.majimechibireminder2.Themes" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="26dp"
    android:text="@string/save" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="260dp"
    android:layout_height="280dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:fillViewport="false"
    android:orientation="vertical"
    android:padding="10dp" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="395dp"
        android:background="#618685" >

        <Button
            android:id="@+id/colorBlue"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="#4d4dff" />

        <Button
            android:id="@+id/colorPurple"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="#d11aff" />

        <Button
            android:id="@+id/colorPink"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="#ff4dc4" />

        <Button
            android:id="@+id/colorRed"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="#ff0000" />

        <Button
            android:id="@+id/colorYellow"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="#ffff00" />

        <Button
            android:id="@+id/colorOrange"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="#ffaa00" />

        <Button
            android:id="@+id/colorGreen"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="#5cd65c" />
    </TableLayout>
</ScrollView>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/scrollView1"
    android:layout_alignLeft="@+id/scrollView1"
    android:layout_marginBottom="24dp"
    android:text="@string/theme_color"
    android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

我的java类中的最新代码

    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    relLayout = (View) findViewById(R.id.relative_theme);
     SharedPreferences preferences = getSharedPreferences("MY_APP",MODE_PRIVATE);
     setTheme(preferences.getInt("MY_CURRENT_THEME",R.style.AppTheme));
    setContentView(R.layout.theme_layout);  

    findViewById(R.id.colorBlue).setOnClickListener(this);
    findViewById(R.id.colorGreen).setOnClickListener(this);
    findViewById(R.id.colorOrange).setOnClickListener(this);
    findViewById(R.id.colorPink).setOnClickListener(this);
    findViewById(R.id.colorPurple).setOnClickListener(this);
    findViewById(R.id.colorRed).setOnClickListener(this);
    findViewById(R.id.colorYellow).setOnClickListener(this);


}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){
    case R.id.colorRed:
        preferences.edit().putInt("MY_CURRENT_THEME",R.style.red_theme).commit(); 
        finish(); startActivity(getIntent());
        break;
    }

}

}

1 个答案:

答案 0 :(得分:1)

更改整个应用程序颜色或主题需要更改应用程序的配置。最初我们需要根据应用中的颜色数量创建样式列表。因此,在styles.xml中更新如下代码,并根据您的应用需求进行相应更改。

<!-- Base application theme. -->
<style name="red_theme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/redprimary</item>
    <item name="colorPrimaryDark">@color/redprimary_dark</item>
    <item name="colorAccent">@color/redaccent</item>
    <item name="android:textColorPrimary">@color/redprimary_text</item>
    <item name="android:textColorSecondary">@color/redsecondary_text</item>
    <item name="android:windowBackground">@color/windowBackground</item>
    <item name="android:navigationBarColor">@color/navigationColor</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="themedMenuMailDrawable">@drawable/ic_menu_camera</item>

</style>


<style name="brown_theme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/brownprimary</item>
    <item name="colorPrimaryDark">@color/brownprimary_dark</item>
    <item name="colorAccent">@color/brownaccent</item>
    <item name="android:textColorPrimary">@color/brownprimary_text</item>
    <item name="android:textColorSecondary">@color/brownsecondary_text</item>
    <item name="android:windowBackground">@color/windowBackground</item>
    <item name="android:navigationBarColor">@color/navigationColor</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="themedMenuMailDrawable">@drawable/ic_menu_manage</item>

</style>


<style name="yellow_theme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/yellowprimary</item>
    <item name="colorPrimaryDark">@color/yellowprimary_dark</item>
    <item name="colorAccent">@color/yellowaccent</item>
    <item name="android:textColorPrimary">@color/yellowprimary_text</item>
    <item name="android:textColorSecondary">@color/yellowsecondary_text</item>
    <item name="android:windowBackground">@color/windowBackground</item>
    <item name="android:navigationBarColor">@color/navigationColor</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="themedMenuMailDrawable">@drawable/ic_menu_slideshow</item>

</style>


<style name="green_theme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/greenprimary</item>
    <item name="colorPrimaryDark">@color/greenprimary_dark</item>
    <item name="colorAccent">@color/greenaccent</item>
    <item name="android:textColorPrimary">@color/greenprimary_text</item>
    <item name="android:textColorSecondary">@color/greensecondary_text</item>
    <item name="android:windowBackground">@color/windowBackground</item>
    <item name="android:navigationBarColor">@color/navigationColor</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="themedMenuMailDrawable">@drawable/ic_menu_gallery</item>

</style>

由于您的样式需要不同的颜色,因此请将它们保存在colors.xml文件中

<?xml version="1.0" encoding="utf-8"?><resources>
<color name="windowBackground">#FFFFFF</color>
<color name="navigationColor">#000000</color>
<color name="colorAccent">#FF4081</color>



<color name="redprimary">#F44336</color>
<color name="redprimary_dark">#D32F2F</color>
<color name="redprimary_light">#FFCDD2</color>
<color name="redaccent">#FF4081</color>
<color name="redprimary_text">#212121</color>
<color name="redsecondary_text">#727272</color>
<color name="redicons">#FFFFFF</color>
<color name="reddivider">#B6B6B6</color>


<color name="brownprimary">#795548</color>
<color name="brownprimary_dark">#5D4037</color>
<color name="brownprimary_light">#D7CCC8</color>
<color name="brownaccent">#9E9E9E</color>
<color name="brownprimary_text">#212121</color>
<color name="brownsecondary_text">#727272</color>
<color name="brownicons">#FFFFFF</color>
<color name="browndivider">#B6B6B6</color>


<color name="greenprimary">#4CAF50</color>
<color name="greenprimary_dark">#388E3C</color>
<color name="greenprimary_light">#C8E6C9</color>
<color name="greenaccent">#FFC107</color>
<color name="greenprimary_text">#212121</color>
<color name="greensecondary_text">#727272</color>
<color name="greenicons">#FFFFFF</color>
<color name="greendivider">#B6B6B6</color>


<color name="yellowprimary">#FFEB3B</color>
<color name="yellowprimary_dark">#FBC02D</color>
<color name="yellowprimary_light">#FFF9C4</color>
<color name="yellowaccent">#448AFF</color>
<color name="yellowprimary_text">#212121</color>
<color name="yellowsecondary_text">#727272</color>
<color name="yellowicons">#212121</color>
<color name="yellowdivider">#B6B6B6</color>

我们使用以下代码更新所有活动,以便在用户打开活动时设置主题。在设置布局之前添加// setContentView()

 SharedPreferences preferences = getSharedPreferences("MY_APP",MODE_PRIVATE);setTheme(preferences.getInt("MY_CURRENT_THEME",R.style.default_theme));          

所以现在当用户点击不同的颜色按钮时,只需设置新主题并通过分别调用完成和启动活动来更新应用程序

preferences.edit().putInt("MY_CURRENT_THEME",R.style.new_theme).commit();
        finish();
        startActivity(getIntent());
相关问题