首页 文章

带有图像的GridView在RelativeLayout中对齐

提问于
浏览
0

我上下搜索了这个论坛,发现了不同的解决方案,但没有一个能按预期工作 . 我甚至试着看看开源的Shelves项目,这似乎与我在这里想要实现的内容相关 .

我有一个带书柜/书架背景的屏幕,并根据需要滚动,下面的代码 . 我的RelativeLayout高度似乎没有被尊重,将其从100dp更改为200dp没有任何区别 . 我基本上需要GridView中的每一行都是完全相同的高度,并垂直对齐缩略图底部,以便书架上的图像网站 . 我通过对ImageView本身的布局宽度/高度进行硬编码来实现这一目标 . 我尝试使用背景颜色,这样我就可以看到发生了什么,但我仍然无法实现一致的外观 . 我也试图将图像缩小到一致的尺寸,假设图像将是不同的形状(有些是正方形,有些是矩形),可以说是100x100 . 即使我使用的是android:layout_alignParentBottom =“true”属性,我也似乎无法将相对于构建单元格的RelativeLayout中的图像对齐 .

谁能告诉我我做错了什么?我觉得我很接近,虽然我提出的可能是完全黑客攻击: - /

这是我现在工作的屏幕截图的链接,蓝色背景颜色用于调试/测试目的 .

BookShelf Screenshot

/res/layout/titles.xml

<com.alex.android.view.BookShelfView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_titles_grid_view"
    android:cacheColorHint="@android:color/transparent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="25dp"
    android:gravity="bottom"/>

ImageAdapter 

class ImageAdapter extends BaseAdapter {
    private Context mContext;
    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return borrowedAssets.size();
    }

    public Object getItem(int position) {
        return borrowedAssets.get(position);
    }

    public long getItemId(int position) {
        return borrowedAssets.get(position).getContentId();
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {

        View cellView;

        if (convertView == null) {  // if it's not recycled, initialize some attributes
            cellView = getLayoutInflater().inflate(R.layout.title_cell, null);
            ImageView image = (ImageView) cellView.findViewById(R.id.title_thumbnail);
            new DownloadImageTask(image).execute(getString(R.string.thumbnails_server_url)+borrowedAssets.get(position).getThumbnail());
        } else {
            cellView = convertView;
        }

        return cellView;
    }
}

/res/layout/title_cell.xml


<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="100dp"
        android:layout_height="100dp">

    <ImageView android:id="@+id/title_thumbnail"
            android:layout_width="100dp"
            android:layout_height="140dp"
            android:paddingTop="25dp"
            android:scaleType="fitStart"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:background="@color/blue_color"
            android:contentDescription="thumbnail"/>

</RelativeLayout>

1 回答

  • 0

    我的RelativeLayout高度似乎没有被尊重,将其从100dp更改为200dp没有任何区别

    在自定义适配器中,使用inflate方法中的 parent 参数,如下所示:

    cellView = getLayoutInflater().inflate(R.layout.title_cell, parent, false);
    

    另外请记住,在当前的 getView 方法中,当没有回收的 ViewconvertView == null )时,你会为 ImageView only 设置图像,所以当你有一个回收的 View 时,你最终会得到相同的图像 .

相关问题