制作一个GridView,根据里面的数据包裹它的高度和宽度
Making a GridView that wraps its height and width according to the data inside
我正在尝试创建一个 gridview,它根据每个单元格中的 ImageView 宽度和高度来调整其宽度和高度。我的脚本似乎不能那样工作。我不知道我做错了什么。我附上了我正在尝试做的示例图像。这些屏幕截图取自 google Play 商店上的一个应用程序。
这是我的 .xml 文件,其中包含将填充数据的 GridView
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text_view_client_newsfeed_xml"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="20dp"
android:layout_centerInParent="true"
android:text="NO ITEM ADDED YET USE DETAIL BUTTON TO GO FURTHER"
android:textColor="#FFFFFF"/>
<GridView
android:id="@+id/list_view_client_newsfeed_xml"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:numColumns="auto_fit"
>
</GridView>
</RelativeLayout>
这是我的适配器 class 的 getView 方法,它下载图像并将其填充到 gridView 中。我正在使用 Picasso 从服务器下载图像。 `
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderClientNewsFeed holder;
if (null == convertView) {
holder = new ViewHolderClientNewsFeed();
convertView = inflater.inflate(R.layout.grid_view_images_client_newsfeed, parent, false);
holder.newsfeedImage = (ImageView) convertView.findViewById(R.id.imageView_clientNewaFeed);
convertView.setTag(holder);
}
else
{
holder = (ViewHolderClientNewsFeed) convertView.getTag();
}
Picasso.with(context)
.load(imageUrls.get(position))
.fit()
.into(holder.newsfeedImage);
return convertView;
}
`
这是我的新闻源列表行布局。
<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/imageView_clientNewaFeed"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
谁能指出我做错了什么。我想在插入的图像中实现一些东西。
如果您的图片没有填满您的视图宽度,那是因为它们基本上小于您的 phone/tablet 宽度。
您基本上需要更改 ImageView
的 scaleType
。
使用 FIT_XY
可以解决问题,但可能会拉伸图像,您也可以尝试 CENTER_CROP
、CENTER_INSIDE
或 FIT_CENTER
.
您可能需要在 ImageView
s
上将 android:adjustViewBounds
设置为 true
如果您尝试将视图创建为交错网格视图,
您可以在 google 上搜索交错网格布局
here 是我发现的。
我正在尝试创建一个 gridview,它根据每个单元格中的 ImageView 宽度和高度来调整其宽度和高度。我的脚本似乎不能那样工作。我不知道我做错了什么。我附上了我正在尝试做的示例图像。这些屏幕截图取自 google Play 商店上的一个应用程序。
这是我的 .xml 文件,其中包含将填充数据的 GridView
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text_view_client_newsfeed_xml"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="20dp"
android:layout_centerInParent="true"
android:text="NO ITEM ADDED YET USE DETAIL BUTTON TO GO FURTHER"
android:textColor="#FFFFFF"/>
<GridView
android:id="@+id/list_view_client_newsfeed_xml"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:numColumns="auto_fit"
>
</GridView>
</RelativeLayout>
这是我的适配器 class 的 getView 方法,它下载图像并将其填充到 gridView 中。我正在使用 Picasso 从服务器下载图像。 `
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderClientNewsFeed holder;
if (null == convertView) {
holder = new ViewHolderClientNewsFeed();
convertView = inflater.inflate(R.layout.grid_view_images_client_newsfeed, parent, false);
holder.newsfeedImage = (ImageView) convertView.findViewById(R.id.imageView_clientNewaFeed);
convertView.setTag(holder);
}
else
{
holder = (ViewHolderClientNewsFeed) convertView.getTag();
}
Picasso.with(context)
.load(imageUrls.get(position))
.fit()
.into(holder.newsfeedImage);
return convertView;
}
` 这是我的新闻源列表行布局。
<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/imageView_clientNewaFeed"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
谁能指出我做错了什么。我想在插入的图像中实现一些东西。
如果您的图片没有填满您的视图宽度,那是因为它们基本上小于您的 phone/tablet 宽度。
您基本上需要更改 ImageView
的 scaleType
。
使用 FIT_XY
可以解决问题,但可能会拉伸图像,您也可以尝试 CENTER_CROP
、CENTER_INSIDE
或 FIT_CENTER
.
您可能需要在 ImageView
s
android:adjustViewBounds
设置为 true
如果您尝试将视图创建为交错网格视图, 您可以在 google 上搜索交错网格布局 here 是我发现的。