使用XML布局文件和java代码共同控制UI界面做一个简易图片浏览器

使用XML布局文件和java代码共同控制UI界面做一个简易图片浏览器

本代码实现图片点击切换和通过按钮点击切换图片的功能:

photo.xml代码如下:
<span style="font-family:Courier New;font-size:14px;"><strong><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <LinearLayout

        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:text="上一张"
            android:id="@+id/bt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/bt2"
            android:text="下一张"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout1"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </LinearLayout>

</LinearLayout></strong></span>

MainActivity.java代码如下:
<strong><span style="font-family:Courier New;font-size:14px;">package com.example.lenovo.photobrowse;

import android.support.annotation.Nullable;
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.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity  {

    //图片资源
    private int[] image = {R.drawable.item1,R.drawable.item2,R.drawable.item3,R.drawable.item4,R.drawable.item5,R.drawable.item6,R.drawable.item7,R.drawable.item8,R.drawable.item9,R.drawable.item10,R.drawable.item11,R.drawable.item12};

    //存放图片的下标
    private int current = 0;

    private Button bt1;
    private Button bt2;


    private LinearLayout mLinearLayout1;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo);

        setTitle("使用XML布局文件和java代码共同控制UI界面");

        //初始化LinearLayout和按钮
        mLinearLayout1 = (LinearLayout)findViewById(R.id.layout1);
        bt1 = (Button) findViewById(R.id.bt1);
        bt2 = (Button)findViewById(R.id.bt2);
        final ImageView imageView = new ImageView(this);

        //布局加载ImageView组件
        mLinearLayout1.addView(imageView);

        //ImageView加载第一张图片
        imageView.setImageResource(image[current]);

        //imageView的点击事件
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (current >= image.length - 1) {
                    current = -1;
                }

                imageView.setImageResource(image[++current]);
            }
        });

        //按钮点击事件
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(current<=0){
                    current = 1;
                    Toast.makeText(MainActivity.this,"图片浏览完毕",Toast.LENGTH_SHORT).show();
                }
                imageView.setImageResource(image[--current]);
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(current>=image.length-1){
                    current = image.length-2;
                    Toast.makeText(MainActivity.this,Toast.LENGTH_SHORT).show();
                }
                imageView.setImageResource(image[++current]);

            }
        });
    }

}</span><span style="font-size:18px;">
</span></strong>

总结:本代码主要是用LinearLayout布局加载ImageView和tough事件来实现,图片资源大家可以自行下载,我这就不提供了。一定会有人问,ImageView放在.XML文件里不就更容易了吗,确实是这样的。我这里主要是提供一个范例,从Activityli也可以实现布局的布置。但是如果单纯使用Layout布局会显得不灵活,只使用java代码的形式又太冗杂。两者结合就很好了。本人是刚学android,故有空就写个帖子。定时更新。不当之处敬请谅解和指正。大神们可以无视。

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念