在 Android Studio 中使用图像按钮时出现问题
Issue with using an image button in Android Studio
我正在创建一个 Soundboard 应用程序,它当前使用按钮来发出不同的声音。但是我想将按钮更改为图像按钮,尝试过后出现以下错误:
意外投射到 ImageButton:布局标记是 imageView。
我试过在互联网上四处寻找,但找不到解决办法。 java class 中的代码如下所示。
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 像普通按钮一样与动作侦听器一起工作,以使应用程序在视觉上更具吸引力。
提前致谢。
在您的 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" />
请注意,我将 background 设为透明色,并将 drawable 设为 src 属性
改为将其转换为 ImageView,因为这就是 XML
中的内容
ImageButton IMG = (ImageButton) findViewById(R.id.IMG);
OnClickListener 仍将按预期工作。
此外,请注意变量阴影。你会想要换行。
IMG = (ImageButton) findViewById(R.id.IMG);
在您中将 IMG 声明为 ImageView activity 或在布局文件中使用 imagebutton 小部件。
我正在创建一个 Soundboard 应用程序,它当前使用按钮来发出不同的声音。但是我想将按钮更改为图像按钮,尝试过后出现以下错误: 意外投射到 ImageButton:布局标记是 imageView。 我试过在互联网上四处寻找,但找不到解决办法。 java class 中的代码如下所示。
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 像普通按钮一样与动作侦听器一起工作,以使应用程序在视觉上更具吸引力。
提前致谢。
在您的 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" />
请注意,我将 background 设为透明色,并将 drawable 设为 src 属性
改为将其转换为 ImageView,因为这就是 XML
中的内容ImageButton IMG = (ImageButton) findViewById(R.id.IMG);
OnClickListener 仍将按预期工作。
此外,请注意变量阴影。你会想要换行。
IMG = (ImageButton) findViewById(R.id.IMG);
在您中将 IMG 声明为 ImageView activity 或在布局文件中使用 imagebutton 小部件。