首页 > 代码库 > Gallery上边图片下边文字显示并且支持大图预览

Gallery上边图片下边文字显示并且支持大图预览

预览图片

技术分享

MainActivity.java

package com.tinybai.android.galleryimgandtext;import android.content.Context;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import android.widget.TextView;/** *  * ClassName: MainActivity <br/> * Function: TODO ADD FUNCTION. <br/> * Reason: TODO ADD REASON(可选). <br/> * date: 2015年1月27日 上午11:18:44 <br/> * * @author caofei 982112095@qq.com * @version  * @since 2015年1月27日 */public class MainActivity extends ActionBarActivity {    private Integer[] img = { R.drawable.jay1, R.drawable.jay2,            R.drawable.jay3, R.drawable.jay4, R.drawable.jay5, R.drawable.jay6,            R.drawable.jay7, R.drawable.jay8 };    private String[] str = { "jay1", "jay2", "jay3", "jay4", "jay5", "jay6",            "jay7", "jay8" };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Gallery gallery = (Gallery) findViewById(R.id.gallery);        gallery.setAdapter(new galleryAdapter(this));        gallery.setSelection(0);//默认显示第一张图片        gallery.setSpacing(20);//图片之间距离        gallery.setOnItemClickListener(new OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view,                    int position, long id) {                // TODO Auto-generated method stub                ImageView imageResult = (ImageView) findViewById(R.id.imageresult);                imageResult.setImageResource(img[position]);            }        });    }    public class galleryAdapter extends BaseAdapter {        private Context mContext;        public galleryAdapter(Context c) {            mContext = c;        }        @Override        public int getCount() {            // TODO Auto-generated method stub            return img.length;        }        @Override        public Object getItem(int position) {            // TODO Auto-generated method stub            return position;        }        @Override        public long getItemId(int position) {            // TODO Auto-generated method stub            return position;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            // TODO Auto-generated method stub            ViewHolder holder;            if (convertView == null) {                holder = new ViewHolder();                convertView = View.inflate(mContext, R.layout.imgandtext, null);                holder.pic = (ImageView) convertView.findViewById(R.id.image);                holder.text = (TextView) convertView.findViewById(R.id.text);                convertView.setTag(holder);            } else {                holder = (ViewHolder) convertView.getTag();            }            holder.pic.setImageResource(img[position]);            holder.text.setText(str[position]);            return convertView;        }        class ViewHolder {            private ImageView pic;            private TextView text;        }    }}

main.xml

<LinearLayout 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:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <Gallery        android:id="@+id/gallery"        android:layout_width="fill_parent"        android:layout_height="wrap_content" />    <ImageView        android:id="@+id/imageresult"        android:layout_width="300dp"        android:layout_height="300dp"        android:layout_gravity="center" /></LinearLayout>

imgandtext.xml

<LinearLayout 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:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin" >    <ImageView        android:id="@+id/image"        android:layout_width="100dp"        android:layout_height="100dp"        android:layout_gravity="center_horizontal" />    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:gravity="center"        android:textSize="12dp" /></LinearLayout>

下载:源码

Gallery上边图片下边文字显示并且支持大图预览