-6

可能重复:
Android 按钮:设置 onClick 背景图像更改与 XML?

当你触摸屏幕改变背景时,我需要它。我有一个 flashlight.xml、FlashlightActivity 和两张图片 background1 和 bacground2。我知道告诉我并不难

我试过:

    public void onClick(View v) {
if (v==Button1)
Button1.setImageResource(R.drawable.background1);

else if Button1.setImageResource(R.drawable.background2);
        }
    });
4

1 回答 1

0

为此,您必须在一个主要线性布局内创建两个布局,并给布局宽度和高度都填充父级。默认情况下使其中一个布局不可见,并使其在另一个布局的布局单击中可见。

<Linearlayout android:layout_width="fill_parent" 
android:layout_height="fill_parent">

  <Linearlayout android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:id="@+id/layout2"
    android:background="@drawable/background1">
  </Linearlayout>

  <Linearlayout android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:id="@+id/layout2"
    android:background="@drawable/background2">
  </Linearlayout>
</Linearlayout>

在您的布局中的活动中单击这样的句柄(不完全是),

// Enable Layout 2 and Disable Layout 1
  Layout1 .setVisibility(View.GONE);
  Layout2.setVisibility(View.VISIBLE);
于 2012-06-16T18:05:01.983 回答