正如您所说...添加图像按钮首先必须在您的可绘制文件夹中添加图像(在您的项目树中搜索它),然后转到要使用它的布局并像这样编写代码(注意:在, android:background="" 必须输入与您在drawable文件夹中复制的图像相同的名称):
<ImageButton
android:id="@+id/arrow"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="4dp"
android:background="@drawable/arrow"
android:contentDescription="@string/test"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.902"
app:layout_constraintStart_toStartOf="parent" />
之后,转到您的 Activity.class 并输入您的代码:
public class MainActivity extends AppCompatActivity {
ImageButton test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test = findViewById(R.id.test);
test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Here should put what you want to show or where you want to go after click the button.
//For example if you want to go to the next activity use Intents like this:
Intent intent = new Intent(this, MainActivity1.class);
startActivity(intent);
finish();
}
});
}
}