我正在上一门关于android的课程,我有一个显示TabLayout
的示例应用程序。应用程序有两个选项卡,第一个是花的图像,第二个是狗的图像。它们都是网格视图。每次我更改标签时,它会将fragment_container
更改为适当的片段,其中包含代码中的照片。
然而,当我运行app图像时彼此重叠。我不知道这有什么问题 - 我试图改变一些属性,例如stretchMode
等,但它没有帮助。
这是它的外观,根据讲师的说法,它应该在这种设备上每行最多显示3张图像。
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
grid_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XXXXX" >
</TextView>
</LinearLayout>
更新。适配器
public class ImageAdapter extends BaseAdapter {
private static final int PADDING = 8;
private static final int WIDTH = 250;
private static final int HEIGHT = 250;
private Context mContext;
private List<Integer> mThumbIds;
public ImageAdapter(Context c, List<Integer> ids) {
mContext = c;
this.mThumbIds = ids;
}
@Override
public int getCount() {
return mThumbIds.size();
}
@Override
public Object getItem(int position) {
return null;
}
// Will get called to provide the ID that
// is passed to OnItemClickListener.onItemClick()
@Override
public long getItemId(int position) {
return mThumbIds.get(position);
}
// create a new ImageView for each item referenced by the Adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = (ImageView) convertView;
// if convertView's not recycled, initialize some attributes
if (imageView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(WIDTH, HEIGHT));
imageView.setPadding(PADDING, PADDING, PADDING, PADDING);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
imageView.setImageResource(mThumbIds.get(position));
return imageView;
}
}
答案 0 :(得分:3)
尝试在适配器中更新getView方法,使图像大小不会硬编码为250。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = (ImageView) convertView;
// if convertView's not recycled, initialize some attributes
if (imageView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imageView.setPadding(PADDING, PADDING, PADDING, PADDING);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
imageView.setImageResource(mThumbIds.get(position));
return imageView;
}