Android學習筆記(二)android studio基本控件及佈局(實現圖片查看器)

我們想要達到的預期效果圖:
在這裏插入圖片描述
下面直接附上實現的代碼:
.xml文件

<?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:id="@+id/activity_hello_world"
    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"
    android:orientation="vertical"
    tools:context="com.example.helloworld.HelloWorld"
    android:weightSum="1">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="204dp"
        android:layout_marginTop="50dp"
        app:srcCompat="@drawable/a"
        android:id="@+id/img" />
    <TextView
        android:text="1/5"
        android:layout_width="match_parent"
        android:layout_height="32dp"
        android:gravity="center"
        android:id="@+id/textView" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_margin="50dp" >
        <Button
            android:id="@+id/lastimg"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="上一張"
            android:textSize="15sp" />
        <Button
            android:id="@+id/nextimg"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:text="下一張"
            android:textSize="15sp" />
    </LinearLayout>
</LinearLayout>

.java文件:

package com.example.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class HelloWorld extends AppCompatActivity implements View.OnClickListener{
    private Button lastimg;
    private Button nextimg;
    private ImageView img;
    private TextView  textView;
    private int[] p = {R.drawable.a, R.drawable.b, R.drawable.c,R.drawable.d, R.drawable.e};
    private int Index = 0;  //圖片的當前索引位置
    private int count = p.length-1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_world);
        init();
    }
    private void init() {
        lastimg = (Button) findViewById(R.id.lastimg);
        lastimg.setOnClickListener(this);
        nextimg = (Button)findViewById(R.id.nextimg);
        nextimg.setOnClickListener(this);
        img =(ImageView) findViewById(R.id.img);
        textView=(TextView)findViewById(R.id.textView);
    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.lastimg:
                //如果當前圖片是第一張,則上一張圖片爲最後一張圖片
                if (Index == 0) {
                    Index = count;
                } else {
                    //否則改爲上一張圖片索引
                    Index = Index - 1;
                }
                break;
            case R.id.nextimg:
                //如果當前圖片是最後一張,則下一張圖片爲第一張圖片
                if (Index == count) {
                    Index = 0;
                } else {
                    //否則改爲下一張圖片索引
                    Index = Index + 1;
                }
                break;
            default:
                break;
        }
        img.setImageResource(p[Index]);   //顯示圖片
        textView.setText(Integer.toString(Index+1)+"/"+p.length);  //顯示圖片是第幾張
    }
}

【上一篇】Android學習筆記(一)——創建第一個Android項目
【下一篇】Android學習筆記(三)android studio中CheckBox自定義樣式(更換複選框左側的勾選圖像)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章