【问题标题】:How to make a galleryview item fill parent width如何使galleryview项目填充父宽度
【发布时间】:2014-07-04 15:05:25
【问题描述】:

我正在尝试使用画廊视图(知道它已弃用)作为水平 GridView。没关系,但现在我想让它一次只显示一个项目,这意味着每个项目都填充父项的宽度。

这是我的 XML(此时极简,只是一个 LinearLayout):

<LinearLayout
        android:id="@+id/profile"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/galleryLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/gallerylabeltext"
            android:layout_marginBottom="10dp"
            android:textSize="@dimen/small_text_size" />

        <Gallery
            android:id="@+id/mygallery"
            android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:gravity="center" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray" />

</LinearLayout>

我怎样才能做到这一点?扩展画廊类?在 XML 中?

提前感谢您的帮助。

【问题讨论】:

    标签: android gridview gallery


    【解决方案1】:

    试试这个用于galary的适配器,

    public class ImageAdapter extends BaseAdapter {
    
            private Context mContext;
    
            public ImageAdapter(Context c) {
                mContext = c;
                aQuery = new AQuery(c); 
            }
    
            public int getCount() {
                return myImageList.length;
            }
    
            public Object getItem(int position) {
                return position;
            }
    
            public long getItemId(int position) {
                return position;
            }
    
            @SuppressWarnings("deprecation")
            public View getView(final int pos, View arg1, ViewGroup arg2) {
                ImageView i = new ImageView(mContext);
                i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                i.setScaleType(ScaleType.FIT_XY);
                i.setImageResource(myImageList[pos]);
                return i;
            }
        }
    

    【讨论】:

    • 即使我不只显示一个 ImageView ?
    • 这个方法确实有效,在我的 convertView 上使用 setLayoutParams,丢弃 setScaleType 和 setImageResource 调用。非常感谢。
    【解决方案2】:

    您可以尝试使用“RelativeLayout”替换 XML 中的“LinearLayout”,我曾经使用 Gallery 一次只显示一项

    【讨论】:

    • 试过了,但对填充宽度没有帮助。也许你可以分享一些代码?谢谢。
    • 我知道你的Gallery为什么不起作用了。如果一个view是Gallery的item,这个view的最外层布局必须使用“RelativeLayout”。
    • 它已经在一个相对布局中。无论如何感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2011-01-15
    • 2018-04-25
    • 2014-08-15
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多