我希望在用户点击按钮时更改布局背景。根据我的项目,我有一个MainActivity,在此MainActivity中我想更改布局背景和一个Gridview活动,在此活动中我有背景图像和一个全屏活动显示gridview选择的特定图像,在此Fullscreen活动中,我将一个按钮名称设置为“设置背景”以设置布局的背景。
这是我的全屏活动
public class FullScreenImage extends Activity{
Button setWallpaper;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_fullscreen_image);
setWallpaper = (Button)findViewById(R.id.setWall);
mContext = FullScreenImage.this;
//Get image fro Image adapter
final Bundle bdl = getIntent().getExtras();
final int imageRes = bdl.getInt("imageID");
final ImageView image = (ImageView) findViewById(R.id.imgDisplay);
image.setImageResource(imageRes);
setWallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//What is the code for changing background
}
});
}
}
Gridview活动
public class ChangeWallpaper extends Activity {
GridView gridImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.change_theme);
gridImage = (GridView)findViewById(R.id.gridImage);
gridImage.setAdapter(new ImageAdapter2(this));
}
}
这是我的Imageadapter,我有背景图片
public class ImageAdapter2 extends BaseAdapter {
private Context mContext;
public ImageAdapter2() {
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return mThumbIds[position];
}
public long getItemId(int position) {
return 0;
}
public ImageAdapter2(Context c) {
mContext = c;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null){
imageView = new ImageView(mContext);
Picasso.with(mContext).load(mThumbIds[position])
.resize(200, 200).into(imageView);
imageView.setLayoutParams(new GridView.LayoutParams(150,150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(3, 3, 3, 3);
imageView.setMaxHeight(300);
imageView.setMaxWidth(300);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyPreferenceActivity myPref = new MyPreferenceActivity(mContext);
myPref.setGifImage(position);
Intent intent = new Intent(mContext,FullScreenImage.class);
intent.putExtra("imageID", mThumbIds[position]);
mContext.startActivity(intent);
}
});
}
else{
imageView = (ImageView) convertView;
}
return imageView;
}
public Integer[] mThumbIds = {
R.drawable.imagess,R.drawable.seven
};
}
这是我想要更改布局背景的MainActivity我的布局是LinearLayout
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
我对图片的偏好活动
public class MyPreferenceActivity extends PreferenceActivity{
Context context;
public SharedPreferences sp;
SharedPreferences.Editor editor;
private final static String KEY_IMAGE = "image";
public MyPreferenceActivity(Context context)
{
this.context = context;
sp = context.getApplicationContext().getSharedPreferences("Image", 0);
editor = sp.edit();
}
public void setGifImage(int imgUrl)
{
editor.putInt(KEY_IMAGE, imgUrl);
editor.commit();
}
public Integer getGifImage()
{
return sp.getInt(KEY_IMAGE, 0);
}
}
所以,如果有人知道这一点。 感谢
答案 0 :(得分:1)
如果背景的更改必须持续,您可以使用 sharedpreferences ,您可以在其中选择要更改prefs的图像,并在活动的 onResume中,后台必须更改您将检查并进行更改
否则你可以创建一个 Singleton ,如果你不需要这个改变是持久的,那就做同样的事情。
编辑
设置首选项
SharedPreferences prefs = context.getSharedPreferences("Image", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(KEY_IMAGE, imgUrl);
editor.commit(); (or .apply(); but in this case you should use commit();)
阅读
SharedPreferences prefs = context.getSharedPreferences("Image", 0);
String url = prefs.getString(KEY_IMAGE, "");
上下文是必需的,但是在活动中你有活动