Android改变不同活动的背景颜色

时间:2014-03-12 01:46:42

标签: android android-layout view

我试图更改主要活动的背景颜色。从菜单中,我选择设置并带我进入另一项设置活动。当我点击我的复选框来更改背景时,它会崩溃。我试图更改主要活动视图的背景。 LinearLayout是主要活动的子项,也是我想要更改的项目的子项。但我一直得到无效指针,任何帮助我可以做些什么来解决这个问题,或者我需要做些什么才能让它发挥作用?

设置活动类

public class ShowSettingsActivity extends Activity {
    static final String TAG = "CAT";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_settings_layout);

        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);

        StringBuilder builder = new StringBuilder();

        builder.append("\n" + sharedPrefs.getBoolean("night_mode_key", true));
        builder.append("\n" + sharedPrefs.getString("time_usage_key", "-1"));
        builder.append("\n" + sharedPrefs.getString("language_key", "-1"));

        //builder.append("\n" + sharedPrefs.getBoolean("time_history_key", true));

        TextView settingsTextView = (TextView) findViewById(R.id.settings_text_view);
        settingsTextView.setText(builder.toString());





    }

}

偏好活动类

public class QuickPrefsActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener{
    CheckBoxPreference isReg;
    static final String TAG = "QuickPrefsActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);        
        addPreferencesFromResource(R.xml.preferences); 

        PreferenceManager.setDefaultValues(this,R.xml.preferences, false);

        CheckBoxPreference  night_checkbox = (CheckBoxPreference) findPreference("night_mode_key");        

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //menu.add(Menu.NONE, 0, 0, "Show current settings");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 0:
                startActivity(new Intent(this, ShowSettingsActivity.class));
                return true;
        }
        return false;
    }
    @Override
    public void onStart(){
        super.onStart();
        SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(QuickPrefsActivity.this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // Set up a listener whenever a key changes
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // Unregister the listener whenever a key changes
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        SharedPreferences s = getSharedPreferences("MY_PREFS", 0);

        // Create a editor to edit the preferences:
        SharedPreferences.Editor editor = s.edit();

        // Let's do something a preference value changes
        if (key.equals("night_mode_key")) 
        {
            // Create a reference to the checkbox (in this case):
            CheckBoxPreference mHints = (CheckBoxPreference)getPreferenceScreen().findPreference("night_mode_key");

            //Lets change the summary so the user knows what will happen using a one line IF statement:
         //mHints.setSummary(mHints.isChecked() ? "Hints will popup." : "No hints will popup.");
            // Lets store the new preference:
            //SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
            boolean checkBoxValue = s.getBoolean("night_mode_key", false);

       if (checkBoxValue) 
             {


                  Log.d(TAG, "true onshared" + checkBoxValue);

                    //setContentView(R.layout.activity_main);
                    View v = findViewById(R.id.LinearLayout1);
                    //View root = v.getRootView();

                    v.setBackgroundColor(0xffffffff);
                  /*((TextView) findViewById(R.id.hanziTextView)).setTextColor(0xff000000);
                  ((TextView) findViewById(R.id.timerTextView)).setTextColor(0xff000000);
                  ((TextView) findViewById(R.id.instructionTextView)).setTextColor(0xff000000);*/

             }
       else
             {
           /* findViewById(R.id.LinearLayout1).setBackgroundColor(0xff000000);
                ((TextView) findViewById(R.id.hanziTextView)).setTextColor(0xff444444);
                ((TextView) findViewById(R.id.timerTextView)).setTextColor(0xff444444);
                ((TextView) findViewById(R.id.instructionTextView)).setTextColor(0xff444444);*/
                Log.d(TAG, "false onshared" + checkBoxValue);
            }                      

         //checkBox.setChecked(false);

         editor.putBoolean("night_mode_key", mHints.isChecked());

        // Save the results:
        editor.commit();
   }

        if (key.equals("language_key")) {

       sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
       //ListPreference listPref = (ListPreference) sharedPreferences; 
       String entryvalue = sharedPreferences.getString( "language_key", "");
       Log.d(TAG, "Entryvalue " + entryvalue);


        if (entryvalue.equals("EN"))
        {
            Log.d(TAG, "EN " + entryvalue);
            Toast.makeText(getBaseContext(), "English Selected", Toast.LENGTH_SHORT).show();
        }
        else if (entryvalue.equals("CH"))
        {
            Log.d(TAG, "CH " + entryvalue);
            Toast.makeText(getBaseContext(), "Chinese Selected", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(getBaseContext(), "You may not go where no1 has gone before, pick again!", Toast.LENGTH_SHORT).show();
        }



       }

    }
 }

主要布局xml ...我试图改变背景

    <android.support.v4.widget.DrawerLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


     <android.gesture.GestureOverlayView
             xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:id="@+id/gestureOverlayView1"
             android:layout_width="match_parent"

             android:layout_height="match_parent" 
             tools:context=".MainActivity">



        <LinearLayout 
            android:id="@+id/LinearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="10dp" 
            android:layout_gravity="start">



        <TextView
            android:id="@+id/timerTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="5sp"
            android:text="@string/startTime"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="30sp" />

</LinearLayout>

</android.gesture.GestureOverlayView>

   <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

   <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#666"
        android:dividerHeight="1dp"
        android:background="#333"
        android:paddingLeft="15sp"
        android:paddingRight="15sp"
        />




</android.support.v4.widget.DrawerLayout>

2 个答案:

答案 0 :(得分:2)

它会崩溃,因为目标LinearLayout在另一个活动中。您可以做的是..在Sharedpreferences文件中保存背景或设置的颜色..然后在主活动中再次调用它以更改颜色......

 if (checkBoxValue) 
         {
            SharedPreferences sp = getSharedPreferences("settings", Context.MODE_PRIVATE);
            Editor edit = sp.edit();
            edit.putString("COLOR", "0xffffffff")
         }

然后在你的主要检查sharedPreferences的值......

...
SharedPreferences sp = getSharedPreferences("settings", Context.MODE_PRIVATE);
v.setBackgroundColor(sp.getString("COLOR"), Color.WHITE); // here check if the value available, if not put the color to default.. maybe white?...

这是未经测试的代码..我粗略地写了..但我希望你有这个想法。 请给我一个反馈

答案 1 :(得分:0)

您无法从其他活动访问主要活动的布局视图。这样做的方法是为再次启动主活动的意图添加额外的内容,或者您​​可以使用SharedPreferences。