【问题标题】:Change background image on button click单击按钮更改背景图像
【发布时间】:2014-05-13 16:53:59
【问题描述】:

我正在使用 eclipse 编写我的 android 应用程序,我想知道如何在单击按钮时更改 MainActivity 的背景图像。我有 img1.png 和 img2.png。背景当前设置在 img1.png 上,xml 代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/img1"
    tools:context=".MainActivity" >
<Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="46dp"
        android:layout_marginTop="55dp"
        android:background="@android:color/transparent"
        android:text="" />

</RelativeLayout>

我只是不确定我将使用什么 java 代码来更改 btn1 点击时的背景图像。

【问题讨论】:

  • 对状态使用drawable

标签: java android eclipse button


【解决方案1】:

此代码可用于以编程方式设置背景图像

RelativeLayout layout =(RelativeLayout)findViewById(R.id.relativelayout);
layout.setBackgroundResource(R.drawable.img1);

【讨论】:

  • 您也可以为线性布局执行此操作,或者这是仅相对布局的功能吗?只是徘徊。
  • 是的,LinearLayout 也可以,LinearLayout layout =(LinearLayout)findViewById(R.id.linearlayout); 也可以这样做
  • 无需投射。 findViewById 返回一个ViewRelativeLayoutLinearLayout 都是Views,setBackgroundResourceView 上的一个方法。
  • 我在这部分(R.id.relativelayout)下得到一条红线;在 relativelayout 部分下面?
  • 检查 xml 文件中相对布局的 id..应该替换为 relativelayout..
【解决方案2】:

这可能是一个解决方案。

在您的布局中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lyt_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img1"
tools:context=".MainActivity" >

在您的活动中

RelativeLayout layout;
public void onCreate(Bundle savedInstanceState){
super.onCreate(Bundle savedInstanceState);
setContentView(your_xml.xml);
layout = (RelativeLayout) findById(R.id.lyt_main);
button = (Button) findById(R.id.lyt_main);
button.setOnClickListener(new OnClickListener{
      public void onClick(View v) {
               layout.setBackgroundDrawable(your_image));
      }
});
}

【讨论】:

  • 我会将 R.id.lyt_main 更改为什么?
【解决方案3】:

将 android ID 添加到您的布局中:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/img1"
        android:id="@+id/rlayout"
        tools:context=".MainActivity" >
...
</RelativeLayout>

现在在您的 MainActivity.java 中:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.btn1);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                RelativeLayout rlayot = (RelativeLayout) findViewById(R.id.rlayout);
                rlayot.setBackgroundResource(R.drawable.img2);
            }
        });
    }

【讨论】:

    猜你喜欢
    • 2011-12-14
    • 2017-03-07
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多