更改按钮上的背景图片,然后单击android应用程序开发 [英] Changing background image on button click android app development
本文介绍了更改按钮上的背景图片,然后单击android应用程序开发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试更改某项活动的背景图片 单击按钮,但无法执行此操作.你们能告诉我我该怎么做吗?
I am trying to change the background image of an activity on button click, but not being able to do so. Can you guys tell me how can I can do that?
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try
{
Activity activity = Selection.this;
//Drawable db = Drawable..createFromPath(R.drawable.love1);
// The below line gives error because setbackgroundImage
// is supposed to take a drawable object as an argument.
// Now what to do?
activity.findViewById(android.R.id.content)
.setBackground(Drawable.createFromPath("res/love1.jpg"));
// What to do to correct the above line?
mySong = MediaPlayer.create(Selection.this, R.raw.song1);
mySong.start();
}
catch(Exception ee){
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(ee.getMessage());
}
}
});
我尝试使用setBackgroundColor对Color.RED进行操作,但这也无法正常工作. PS:我没有更改xml文件中的任何内容来完成此操作.
I tried with Color.RED using setBackgroundColor but that too is not working. PS: I have not changed anything in the xml file for accomplishing this.
推荐答案
我认为最简单的方法是使用xml
文件中的布局进行活动.例如:
I think the easiest way is to use layout in your xml
file for your activity.. for example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayoutID"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background_img1" >
,然后在需要时从活动"中将其更改单击按钮后:
and then change it from your Activity when you want e.g. after the button click:
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linearLayoutID);
mLinearLayout.setBackgroundResource(R.drawable.background_img2);
这篇关于更改按钮上的背景图片,然后单击android应用程序开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文