尝试使用首选项框架更改活动的背景颜色

时间:2015-02-27 11:27:28

标签: android preferenceactivity

嗨,我是新来的android我有这个小应用程序,将主Activity的背景颜色更改为红色..使用Preference framework.but它无法正常工作请帮助

注意:我正在使用Genymotion模拟器

主要活动中的

代码

package com.example.prefactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PreferenceManager.setDefaultValues(getApplicationContext(),R.xml.myprefs, false);

    }

    @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) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            startActivity(new Intent(this,SettingsActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
设置活动中的

代码

package com.example.prefactivity;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

public class SettingsActivity extends PreferenceActivity {

    final static String RED = "red";
    //final static String FONT = "font";
    OnSharedPreferenceChangeListener preflistner;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.myprefs);
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
         preflistner = new OnSharedPreferenceChangeListener() {

            @Override
            public void onSharedPreferenceChanged(SharedPreferences mpref,
                    String key) {
                LayoutInflater inflater = getLayoutInflater();
                ViewGroup container = null;
                View view=inflater.inflate(R.layout.activity_main, container);
                RelativeLayout mylayout = (RelativeLayout)view.findViewById(R.id.myrellayout);
                if(key.equals(SettingsActivity.RED)){

                    boolean color = mpref.getBoolean(SettingsActivity.RED, false);
                    if(color==true)
                        mylayout.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));

                    else
                        mylayout.setBackgroundColor(getResources().getColor(android.R.color.background_light));
                }

            }
        };
        pref.registerOnSharedPreferenceChangeListener(preflistner);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

logcat文件



02-27 10:49:13.300: D/libEGL(1589): loaded /system/lib/egl/libEGL_genymotion.so
02-27 10:49:13.324: D/(1589): HostConnection::get() New Host Connection established 0xb886cf40, tid 1589
02-27 10:49:13.392: D/libEGL(1589): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-27 10:49:13.412: D/libEGL(1589): loaded /system/lib/egl/libGLESv2_genymotion.so
02-27 10:49:13.496: W/EGL_genymotion(1589): eglSurfaceAttrib not implemented
02-27 10:49:13.520: E/OpenGLRenderer(1589): Getting MAX_TEXTURE_SIZE from GradienCache
02-27 10:49:13.524: E/OpenGLRenderer(1589): MAX_TEXTURE_SIZE: 16384
02-27 10:49:13.596: E/OpenGLRenderer(1589): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-27 10:49:13.628: E/OpenGLRenderer(1589): MAX_TEXTURE_SIZE: 16384
02-27 10:49:13.628: D/OpenGLRenderer(1589): Enabling debug mode 0
02-27 10:49:25.656: D/dalvikvm(1589): GC_FOR_ALLOC freed 142K, 8% free 3134K/3384K, paused 9ms, total 11ms
02-27 10:49:25.672: D/dalvikvm(1589): GC_FOR_ALLOC freed 6K, 8% free 3225K/3484K, paused 4ms, total 5ms
02-27 10:49:25.700: I/dalvikvm-heap(1589): Grow heap (frag case) to 4.328MB for 1127532-byte allocation
02-27 10:49:25.712: D/dalvikvm(1589): GC_FOR_ALLOC freed <1K, 6% free 4326K/4588K, paused 12ms, total 12ms
02-27 10:49:25.876: W/EGL_genymotion(1589): eglSurfaceAttrib not implemented
02-27 10:49:27.944: W/EGL_genymotion(1589): eglSurfaceAttrib not implemented
02-27 10:49:28.644: I/Choreographer(1589): Skipped 46 frames!  The application may be doing too much work on its main thread.
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

尝试以这种方式实施OnpreferenceChangeListener 那么你可以在偏好改变时做你想做的事。请参阅示例代码。

Preference.OnPreferenceChangeListener changeListener = new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
    // Code goes here            
    return true;
}
};

然后,您尝试通过调用

更改活动的背景颜色
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);

这是完整的代码。

 Preference.OnPreferenceChangeListener changeListener = new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
    changecolor(Color.RED);       
    return true;
}
};



public void changecolor(int color){
      View view = this.getWindow().getDecorView();
      view.setBackgroundColor(color);
}

我希望这适合你。