如何跨活动更改背景颜色

时间:2013-04-15 06:29:36

标签: android sdk background

这是我的第一个应用程序,也是我的第一次发帖,所以请耐心等待。

我正在努力让用户可以从Change Skins屏幕中选择整个应用程序的背景颜色。但是,根据我现在的情况,它只会更改活动的颜色,直到它返回到主要活动。

以下是Change Skins屏幕的代码

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();
            }
        });

    }
}

这是主要的活动代码

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);
    }

}

谢谢!

2 个答案:

答案 0 :(得分:2)

将其放在xml

android:background="#025cd8" 

取决于您选择的颜色。

答案 1 :(得分:1)

您可以执行的操作是将SharedPreferences值设置为用户选择的值,并在每个活动开始时将该值设置为布局的背景颜色。这是我看到解决这个问题的最好方法。

因此,在所有活动中,您可以将它们设置为SharedPreferences中的值,如果未设置,则使用默认值。至于存储可能的值,您可以使用XML文件,也可以只在一个全局类中对值进行硬编码,尽管前者更可取。

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

编辑: 如果你想保持简单,只需背景颜色,那么你可以这样做:

| - 保持这些有点全局,可能在一个类中,并根据需要访问它们,

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

| - 对于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.

| - 至于您的其他活动,或所有活动的onCreate()

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