android - 在 Android Studio 中使用图像按钮的问题

标签 android imagebutton android-imagebutton

我正在创建一个 Soundboard 应用程序,它当前使用按钮来发出不同的声音。但是我想将按钮更改为图像按钮,尝试过后出现以下错误: 意外转换到 ImageButton:布局标记是 imageView。 我试过在互联网上四处寻找,但找不到解决方法。 java 类中的代码如下所示。

 public class SecondActivity extends AppCompatActivity {
    Button b,b2;
    MediaPlayer nice,burp,fancy;
    ImageButton IMG;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        b = (Button) findViewById(R.id.button);
        b2 = (Button) findViewById(R.id.button3);
        nice = MediaPlayer.create(this, R.raw.nice);
        burp = MediaPlayer.create(this, R.raw.burp);
        fancy = MediaPlayer.create(this, R.raw.fancy);
        configureImageButton();

        b.setOnClickListener(new View.OnClickListener() {

                                 public void onClick(View view) {
                                     nice.start();
                                 }


                             }


        );
        b2.setOnClickListener(new View.OnClickListener() {

                                             public void onClick(View view) {
                                                 fancy.start();
                                             }


                                         }

        );
    }

        private void configureImageButton() {
        ImageButton IMG = (ImageButton) findViewById(R.id.IMG);


        IMG.setOnClickListener(new View.OnClickListener() {

                                   public void onClick(View view) {
                                       burp.start();
                                   }


                               }

        );
    }

XML 代码如下所示:

    <?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.oliver.olliessoundofmusic.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nice"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fancy"
        android:id="@+id/button3"
        android:layout_alignTop="@+id/button"
        android:layout_toEndOf="@+id/button" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/IMG"
        android:background="@drawable/robert"
        android:layout_toEndOf="@+id/button3" />
    </RelativeLayout>

我只是想让 imageButtons 像普通按钮一样与 Action 监听器一起工作,使应用程序在视觉上更具吸引力。

提前致谢。

最佳答案

在您的 xml 中,将 ImageView 标记更改为 ImageButton

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Nice"
    android:id="@+id/button"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Fancy"
    android:id="@+id/button3"
    android:layout_alignTop="@+id/button"
    android:layout_toEndOf="@+id/button" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/IMG"
    android:background="@android:color/transparent"
    android:src="@drawable/robert"
    android:layout_toEndOf="@+id/button3" />

请注意,我将背景设为透明色,并将drawable设为src属性

关于android - 在 Android Studio 中使用图像按钮的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37104887/

相关文章:

android - 如何正确扩展ImageButton?

java - 单击按钮时出现 AnimationDrawable 错误

java - 在Android中发出POST请求

getActiveNetworkInfo 之后 android 唤醒锁未释放

android - 区域设置更改时如何刷新现有状态栏通知消息

android - 在android中使用AESCrypt加密和解密

android - 删除自定义 ListView 中的多个选定项目不适用于 ImageButton

java - 在 Android 应用程序中从 ImageButton 打开图库

android - 在方向更改时旋转 ImageButton

安卓工作室项目 : bottom of UI is cut off