刚开始学习Android真的是头很大,需要学习和理解的东西太多,还好网上可以四处搜索加强自己的理解和学习,多的不说上图片上代码。

实际效果是这样的:


布局文件:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android/apk/res/android"
    xmlns:tools="http://schemas.android/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/imagename"
        android:textColor="@color/colorImage"
        android:textSize="25sp"/>

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:contentDescription="@string/app_name" />

    <Button
        android:id="@+id/pre"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/next"
        android:layout_alignBottom="@+id/next"
        android:layout_marginStart="80dp"
        android:text="@string/Previousname" />

    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginStart="250dp"
        android:layout_marginTop="400dp"
        android:text="@string/Nextname" />
</RelativeLayout>

实现代码如下:

MainActivity.java

package com.example.imageview;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.support.v7.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button pre;
    private Button next;
    private ImageView imgView;

    private int[] photos = {R.drawable.image1, R.drawable.image2, R.drawable.image3
            , R.drawable.image4, R.drawable.image5, R.drawable.image6};
    private int photoIndex = 0;
    private int maxIndex = 5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pre = (Button) findViewById(R.id.pre);
        next = (Button)findViewById(R.id.next);
        imgView = findViewById(R.id.imgView);
        pre.setOnClickListener(this);
        next.setOnClickListener(this);
        imgView.setImageResource(photos[3]);
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.pre:
                if (photoIndex == 0)
                {
                    photoIndex = maxIndex;
                }
                else
                {
                    photoIndex = photoIndex - 1;
                }
                break;
            case R.id.next:
                if (photoIndex == maxIndex)
                {
                    photoIndex = 0;
                }
                else
                {
                    photoIndex = photoIndex + 1;
                }
                break;
            default:
                break;
        }
        //显示图片
        imgView.setImageResource(photos[photoIndex]);
    }
}

其中添加图片的话需要自己下载一些图片放到drawable路径下,之后才能进行引用


引用存放在string.xml文件中的文言
android:text="@string/Nextname"

一些小的细节可以自己来修改修饰。。

更多推荐

Android ImageView实现上一页,下一页图片切换