关闭

使用HorizontalScrollView和ImageView编写简单的图片展示程序

标签: imageviewHorizontalScrollView
57人阅读 评论(0) 收藏 举报

程序运行结果截图:

使用HorizontalScrollView和ImageView编写简单的图片展示程序运行程序截图









​MainActivity.java代码如下:

package com.example.helloworld;

import android.os.Bundle;

import android.app.Activity;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup.LayoutParams;

import android.widget.ImageView;

import android.widget.LinearLayout;

public class MainActivity extends Activity 

{

         private ImageView imgv

         LinearLayout h1;

         int [] imgs=

            {

                  R.drawable.img1,

                  R.drawable.img2,

                  R.drawable.img3,

                  R.drawable.img4,

                  R.drawable.img5,

                  R.drawable.img6,

                  R.drawable.img7,

                  R.drawable.img8

             };

        ImageView vs[];

        @Override

        protected void onCreate(Bundle savedInstanceState) 

        {

             super.onCreate(savedInstanceState);

             setContentView(R.layout.activity_main);

             h1=(LinearLayout)findViewById(R.id.h1);

             imgv=(ImageView)findViewById(R.id.imgv);

             imgv.setImageResource(R.drawable.img1);     

             vs=new ImageView[]

                {

                      new ImageView(this),

                      new ImageView(this),

                      new ImageView(this),

                      new ImageView(this),

                      new ImageView(this),

                      new ImageView(this),

                      new ImageView(this),

                      new ImageView(this)

                  };

            for(int i=0;i< vs.length;i++)

           {

               vs[i].setImageResource(imgs[i]);

               vs[i].setLayoutParams(new LayoutParams(120,120));

               vs[i].setOnClickListener(new hkf());

               h1.addView(vs[i]);

            }

      }

    class hkf implements OnClickListener

    {

          public void onClick(View v)

          {

              int i;

              for(i=0;i< vs.length;i++)

              {

                   if(v==vs[i])break;

              }

              imgv.setImageResource(imgs[i]);

            }

     }

​}​


activity_main​.xml代码如下:


 

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    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"

    tools:context="com.example.hkf4.MainActivity" >

 

    ImageView

       android:id="@+id/imgv"

       android:layout_width="400dp"

       android:layout_height="400dp" />

    

    HorizontalScrollView 

      android:layout_width="fill_parent"

      android:orientation="horizontal"

      android:layout_height="wrap_content">  

        LinearLayout

            android:id="@+id/h1"

            android:layout_width="fill_parent"

            android:layout_height="fill_parent"

            android:orientation="horizontal" >          

       </ LinearLayout>       

    </ HorizontalScrollView>

</ RelativeLayout>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:132次
    • 积分:4
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章存档