安卓:使背景图片多变 [英] Android: make background image changable

查看:112
本文介绍了安卓:使背景图片多变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与背景图像中的Andr​​oid应用程序。我的目标是创造3或4的背景图像和给予的可能性的用户改变背景

I have an android app with an background image. My goal is to create 3 or 4 background images and give the possibility to the user to change the background.

其实,我有一个名为bg1.png,bg2.png,bg3.png和bg4.png 4图像。
我已经想东西strings.xml中声明一个字符串值背景与图像的名称。背景图像的名称将永诺从背景串检取并且当用户将改变背景,导向这个更改字符串背景只值

Actually, I have 4 images named bg1.png, bg2.png, bg3.png and bg4.png. I have think about something to declare an string value "background " in Strings.xml with the name of the image. The name of the background image will allways be take from the background string and when the users will change the background, this wil change only the value of the string background.

这是很好的想法,还是有一些easyer?
我怎么可以设置我的布局在XML布局文件有背景的字符串值背景? Shuld我这样做编程?

Is this idea good or is there something easyer? How could I set the background of my layout in the xml layout file with the "background" string's value ? Shuld i do this programatically?

感谢您。

推荐答案

我想,如果你想在后台更改为活动布局,可以使用的setBackground方法的布局为例做到这一点:

I think if you want to change the background to the activity layout, you can do it using setBackground method for the Layout for example:

activityLayout = (LinearLayout)findViewById(R.id.tableLayout1);
activityLayout.setBackgroundDrawable(getResources().getDrawable(R.id.somedrawable))

您可以使用例如共享preference存储的背景图像,那么当你启动一个活动,您读取包含后台preference。例如,当用户选择的背景:

You can store the background image using for example SharedPreference, then when you launch an activity you read the preference that contains the background. For example when the user choice the background:

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefEditor.putInt("backgroudResourceId", userchoice);
prefEditor.commit();

而当活动开始时,必须从共享preference阅读RESOURCEID:

And when an activity start, you must read the resourceId from the SharedPreference:

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
drawableid = myPrefs.getInt("backgroundResourceId", defaultvalue); 
yourlayout.setBackground(drawableid);

,其中设置defaultValue如果preference没有设置默认值。 yourLayout应该被初始化(在活动布局的方法相同)。

where defaultvalue is the default value if the preference is not set. yourLayout is supposed to be initialized (in the same way of activity layout).

这篇关于安卓:使背景图片多变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆