ImageView + Button 实现图片的切换功能(不少于 10 张);
点击图片即可切换
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.a86189.myapplication2.MainActivity"> <ImageView android:id="@+id/image1" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center_horizontal" android:layout_margin="10dp" android:src="@drawable/a" /> <Button android:id="@+id/but1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="10dp" android:text="切换图片"/> </LinearLayout>
package com.example.a86189.myapplication2; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private ImageView image_1; private Button but_1; private int id=R.drawable.a; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image_1=(ImageView) findViewById(R.id.image1); but_1=(Button) findViewById(R.id.but1); but_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(id==R.drawable.a) id=R.drawable.b; else if(id==R.drawable.b) id=R.drawable.c; else if(id==R.drawable.c) id=R.drawable.d; else if(id==R.drawable.d) id=R.drawable.e; else if(id==R.drawable.e) id=R.drawable.f; else if(id==R.drawable.f) id=R.drawable.g; else if(id==R.drawable.g) id=R.drawable.h; else if(id==R.drawable.h) id=R.drawable.i; else if(id==R.drawable.i) id=R.drawable.j; else { id=R.drawable.k; } image_1.setImageResource(id); } }); } }
最后还需将图片放入drawable
⭐⭐⭐持续更新.....
⭐⭐⭐每天提醒自己,自己就是个菜鸡!
⭐⭐⭐如果对您有帮助留下的每一个点赞、收藏、关注是对菜鸡创作的最大鼓励❀
⭐⭐⭐有相关问题可以写在评论区,一起学习,一起进步。