How to change background color across activities

2019-08-07 12:04发布

问题:

This is my first app and my first time posting so bear with me.

I'm trying to make it so the user can choose the background color of the entire app from a Change Skins screen. However with what I have now it only changes the color of the activity until it goes back to the main activity.

Here is the code for the Change Skins screen

package cs.pacificu.mypace;

import android.R.layout;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class Skin extends Activity
{

    @Override
    public void onCreate(Bundle savedInstanceState)
{
    final String[] SKINS = new String[] {"Light", "Dark"};

    super.onCreate(savedInstanceState);
    setContentView(R.layout.playlist);
    final ListView listView = (ListView) findViewById (R.id.playlists);

    listView.setAdapter(new ArrayAdapter<String>            (this,R.layout.single_list_item,SKINS));

    listView.setTextFilterEnabled(true);
    listView.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id)
        {
            // When clicked, show a toast with the TextView text
            Toast.makeText(getApplicationContext(),
            ((TextView) view).getText(), Toast.LENGTH_SHORT).show();

            //
            View layout = findViewById(R.layout.activity_main);
            String selectedFromList = (listView.getItemAtPosition(position).toString ());

            if (SKINS [0] == selectedFromList)
            {
                listView.setBackgroundColor(Color.CYAN);
                 //layout.setBackgroundColor(android.R.color.darker_gray);

            }
            else if (SKINS[1] == selectedFromList)
            {
                listView.setBackgroundColor(Color.BLACK);
                //layout.setBackgroundColor(android.R.color.black);
            }


                //

                finish();
            }
        });

    }
}

And here is the main activity code

package cs.pacificu.mypace;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
/*import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuInflater;*/
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        startPlaylists(null);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item)
    {
        switch (item.getItemId()) {
        case R.id.skin:
            changeSkin();
            return true;
        case R.id.action_settings:
            settingsList();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public void startPlaylists (View view)
    {
        Intent intentPlaylists = new Intent();
        intentPlaylists.setClassName("cs.pacificu.mypace", "cs.pacificu.mypace.Playlist");
        intentPlaylists.setAction("@strings/action_playlists");
        startActivity(intentPlaylists);
    }

    public void changeSkin ()
    {
        Intent intentSkin = new Intent();
        intentSkin.setClassName("cs.pacificu.mypace", "cs.pacificu.mypace.Skin");
        intentSkin.setAction("@strings/action_skin");
        startActivity(intentSkin);
    }

    public void settingsList ()
    {
        Intent intentSettings = new Intent();
        intentSettings.setClassName("cs.pacificu.mypace", "cs.pacificu.mypace.Settings");
        intentSettings.setAction("@strings/action_settings");
        startActivity(intentSettings);
    }

}

Thank you!

回答1:

what you can do is set a SharedPreferences value to what the user selects, and at the start of each of your activities, set that value to the background colour of your layout. That is the best way I see to solving this.

So in all of your activities, you would set them to the value from the SharedPreferences and if not set use your default one. As for storing the possible values, you can either use the XML file or just hard code the values in one global class, although the former is preferable.

Look here and see what they did (Android-page-about-data-storage)

Edit: If you are going to keep it simple like that, just background colour, then you can do something like this:

|- Keep these somewhat global, maybe in a class and access them as needed,

// Make sure to maintain the connection
String[] SKINS = {"Light", "Dark"};
int[] COLOURS = {Color.CYAN, Color.BLACK};

|- For the onItemClick(),

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
    // You may wish to make these two variables class-global
    SharedPreferences settings = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();

    editor.putInt("mskin", COLOURS[position]);
    editor.commit();

    // For on the spot changes
    listView.setBackgroundColor(COLOURS[position]);

    // Your other code, as you wish, before or after, depends on how you need it.

|- As for your other activities, or at the onCreate() of all activities,

SharedPreferences settings = getPreferences(MODE_PRIVATE);
listView.setBackgroundColor(settings.getInt("mskin", COLOURS[0]));


回答2:

Put this on your xml

android:background="#025cd8" 

Depending on your choice of color.