如何改变整个活动的背景颜色(How to change background color acros

2019-10-18 00:49发布

这是我的第一个应用程序,我第一次发布这样忍耐一下。

我想使它这样用户可以选择从更改外观屏幕的整个应用程序的背景颜色。 但是与我有什么,现在它只有等到它可以追溯到主要活动转变活动的颜色。

下面是更改外观屏幕的代码

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

}

谢谢!

Answer 1:

你能做的就是设置一个SharedPreferences值,用户选择什么,并在每个活动的开始,该值设置为你的布局的背景颜色。 这是我看到解决这个的最佳方式。

因此,在所有的活动中,你将它们设置为从价值SharedPreferences ,如果没有设置使用默认值。 至于存储的可能值,您可以使用XML文件或只是硬编码在一个全球一流的价值,尽管前者是优选的。

看看这里,看看他们做了什么(Android的页面有关数据存储部)

编辑:如果你要保持它的简单那样,只是背景颜色,那么你可以做这样的事情:

| - 保留这些有些全球性的,也许在一类,并根据需要访问它们,

// 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]));


Answer 2:

把这个在你的xml

android:background="#025cd8" 

根据您选择的颜色。



文章来源: How to change background color across activities