android - 如何在 xml 和类中将图像设置为按钮

标签 android imagebutton

我是 android 新手..请指导我..

我想设置图像作为按钮..

如何在xml和类文件中实现这个?

我现在在 xml 和类文件中设置按钮的代码是这样的..

  <Button
     android:id="@+id/nxt_btn"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentRight="true"
     android:layout_below="@+id/rdtxt"
     android:layout_marginRight="22dp"
     android:layout_marginTop="34dp"
     android:text="Next" />
     Button nextBtn = (Button) findViewById(R.id.nxt_btn);

我想要 practice.png 这个图像想充当按钮..我已经把这个图像放到 drawables 中..

现在如何通过代码实现这个...请指导我...

非常感谢...

最佳答案

在xml中你可以添加:

<ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image" />

在 Activity 中:

ImageView image = (ImageView) findViewById(R.id.image);
image.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                  //Do your stuff here
            }
        });

关于android - 如何在 xml 和类中将图像设置为按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14751761/

相关文章:

android - 上传图片错误: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference

java - Android - 在 Google PlayMarket 中升级应用程序时,Package files 文件夹会被删除吗?

java - 创建 Android Studio 项目时出错

图像的 Jquery 按钮集大小问题

android - 动态更改源后,Xamarin Forms ImageButton 变小

ios - 无法将图像(个人资料图片)按钮添加到 UINavigationBar

android - 如何在 Android 通知上添加时间戳?

JavaScript 不在模拟器中执行(Android 中的 PhoneGap)

android - 如何在 Android 中的图像按钮下添加文本?

Android TableLayout 拉伸(stretch)单元格内容