Android gallery with caption

前端 未结 1 1549
感动是毒
感动是毒 2020-12-10 00:11

I have been following the gallery example here: http://developer.android.com/resources/tutorials/views/hello-gallery.html

and adding a text to show up under the imag

相关标签:
1条回答
  • 2020-12-10 01:07

    I have tried this one and found one way to solve this. Please try this also:

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center" xmlns:android="http://schemas.android.com/apk/res/android">
        <Gallery android:id="@+id/galleryid"
            android:layout_width="fill_parent" 
            android:layout_height="match_parent"/>      
    </LinearLayout>
    

    image_gallery_items.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content" android:padding="3sp" android:orientation="vertical" android:gravity="center_horizontal"> 
    
        <ImageView android:id="@+id/image" android:src="@drawable/icon"
            android:layout_height="110dp" android:layout_width="130dp" android:layout_gravity="center"></ImageView>
        <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    
    </LinearLayout>
    

    GalleryExample.java

     public class GalleryExample extends Activity {
            private  Gallery galleryView;
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                galleryView = (Gallery)findViewById(R.id.galleryid);
                galleryView.setAdapter(new ImageAdapter(this));
            }
    }
    

    ImageAdapter.java

     public class ImageAdapter extends BaseAdapter {
    
            private Activity activity;
            private static LayoutInflater inflater=null;
    
            public ImageAdapter(Activity a) {
                activity = a;
                inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            }
    
            public int getCount() {
                return data.length;
            }
    
            public Object getItem(int position) {
                return position;
            }
    
            public long getItemId(int position) {
                return position;
            }
    
            public static class ViewHolder{
                public TextView text;
                public ImageView image;
            }
    
            public View getView(int position, View convertView, ViewGroup parent) {
                View vi=convertView;
                ViewHolder holder;
                if(convertView==null){
                    vi = inflater.inflate(R.layout.image_gallery_items, null);
                    holder=new ViewHolder();
                    holder.text=(TextView)vi.findViewById(R.id.textView1);
                    holder.image=(ImageView)vi.findViewById(R.id.image);
                    vi.setTag(holder);
                }
                else
                    holder=(ViewHolder)vi.getTag();
                holder.text.setText(name[position]);
                final int stub_id=data[position];
                holder.image.setImageResource(stub_id);
                return vi;
            }
    
            private int[] data = {
                    R.drawable.imag1, R.drawable.imag2,
                    R.drawable.imag3, R.drawable.imag4,
                    R.drawable.imag5, R.drawable.imag6
            };
            private String[] name = {
                    "Image1", "Image2",
                    "Image3", "Image4",
                    "Image5", "Image6"
            };
        }
    

    And in the AndroidMAnifest.xml specify GalleryExample only.

    0 讨论(0)
提交回复
热议问题