Android的;通过点击一个按钮来更改布局/画面的背景 [英] Android; change the background of a layout/screen by clicking a button

查看:982
本文介绍了Android的;通过点击一个按钮来更改布局/画面的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有几个按钮,我想一个应用程序屏幕的背景,具体取决于我点击了哪个按钮改变。所以,如果我点击与写作蓝色的按钮时,背景变为我已经在res文件夹中创建一个指定的图像。先谢谢了。

So I have several buttons, and I want the background of an app screen to change depending on which button I click. So if I click a button with the writing "blue", the background changes to a specified image which I've created in the res folder. Thanks in advance.

推荐答案

要使用code更改图片

To change image by using code

public void onClick(View v) {
if(v==ButtonName)
ButtonName.setImageResource(R.drawable.ImageName); 
}

或使用XML文件

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"
   android:drawable="@drawable/login_selected" /> <!-- pressed -->
<item android:state_focused="true"
   android:drawable="@drawable/login_mouse_over" /> <!-- focused -->
<item android:drawable="@drawable/login" /> <!-- default -->

的OnClick只是添加此code

OnClick just add this code

ButtonName.setBackgroundDrawable(getResources().getDrawable(R.drawable.ImageName))

即使你想我的背景绘制,这里是:

Even if you want me background drawable, here it is:

android:background="@drawable/ImageName"

现在,你可以加重他们

这篇关于Android的;通过点击一个按钮来更改布局/画面的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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