GridView与图像对齐在RelativeLayout底部

问题描述:

我搜索了这个论坛上下,发现不同的解决方案,但没有一个按预期工作。我甚至试着看看开源的Shelves项目,这似乎与我在这里想要实现的目标有很大关系。GridView与图像对齐在RelativeLayout底部

我有一个书架/书架背景的屏幕,并根据需要使用下面的代码进行滚动。我的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> 

我RelativeLayout的高度似乎并不被认可,并从100dp将其更改为200dp没有什么区别

在您的自定义适配器使用parent参数在充气方法如下:

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

也请记住,在当前的getView方法您为ImageView图像时,不存在回收ViewconvertView == null),所以当你有一个循环View你会用相同的图像结束。