带有标题的Android图库

时间:2011-06-23 00:28:08

标签: android android-activity gallery

我一直关注这里的图库示例:http://developer.android.com/resources/tutorials/views/hello-gallery.html

并添加一个文字显示在此处的图片下方:http://mytelcoit.com/2010/02/programming-android-create-icon-with-text-using-gridview-and-layout-inflater/

但是,我从LogCat

收到此错误
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): FATAL EXCEPTION: main
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): java.lang.ClassCastException: android.widget.Gallery$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:587)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.view.View.measure(View.java:10828)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.view.ViewGroup.measureChild(ViewGroup.java:4322)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:206)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.view.View.measure(View.java:10828)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.view.View.measure(View.java:10828)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:764)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.view.View.measure(View.java:10828)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1890)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.view.View.measure(View.java:10828)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.view.ViewRoot.performTraversals(ViewRoot.java:909)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.view.ViewRoot.handleMessage(ViewRoot.java:2003)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.os.Looper.loop(Looper.java:132)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at android.app.ActivityThread.main(ActivityThread.java:4025)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at java.lang.reflect.Method.invokeNative(Native Method)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at java.lang.reflect.Method.invoke(Method.java:491)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444):     at dalvik.system.NativeStart.main(Native Method)

我假设画廊视图不适用于线性布局?我相信我从getView()获得上述错误。有人知道解决这个问题的方法吗?

GalleryActivity.java

package android.gallery;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.LayoutInflater;
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;
import android.widget.Toast;

public class GalleryActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));

        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                Toast.makeText(GalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;

        private Integer[] mImageIds = {
                R.drawable.sample_1,
                R.drawable.sample_2,
                R.drawable.sample_3,
                R.drawable.sample_4,
                R.drawable.sample_5,
                R.drawable.sample_6,
                R.drawable.sample_7
        };

        public ImageAdapter(Context c) {
            mContext = c;
            TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.HelloGallery_android_galleryItemBackground, 0);
            a.recycle();
        }

        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View v;

            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.caption, null);

            TextView tv = (TextView) v.findViewById(R.id.caption_text);
            tv.setText("test");

            ImageView i = (ImageView)v.findViewById(R.id.image_icon);
            i.setImageResource(mImageIds[position]);
            i.setLayoutParams(new Gallery.LayoutParams(150, 100));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setBackgroundResource(mGalleryItemBackground);

            return v;
        }
    }
}

caption.xml

<?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">

    <ImageView
        android:id="@+id/image_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ImageView>

    <TextView 
        android:text="TextView" 
        android:id="@+id/caption_text" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>

</LinearLayout>

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<Gallery 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">   
</Gallery>

1 个答案:

答案 0 :(得分:4)

我试过这个,找到了解决这个问题的方法之一。请试试这个:

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"
        };
    }

在AndroidMAnifest.xml中仅指定GalleryExample。