我在设置一个装满图片的GridView时遇到了问题。
显示图像时,它们之间存在巨大差距。我已经查找并尝试了许多解决方案,但似乎仍无法找到解决办法。
它看起来像什么:
这是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<GridView
android:id="@+id/list"
style="@style/PhotoGridLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:columnWidth="@dimen/image_thumbnail_size"
android:horizontalSpacing="@dimen/image_thumbnail_spacing"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="@dimen/image_thumbnail_spacing" />
</RelativeLayout>
主要活动:
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
public class MainActivity extends Activity {
GridView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (GridView) findViewById(R.id.list);
adapter = new LazyAdapter(this, mStrings);
list.setAdapter(adapter);
}
@Override
public void onDestroy() {
list.setAdapter(null);
super.onDestroy();
}
private String[] mStrings = {
"www.URLs.jpg" };
}
LazyAdapter:
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private String[] data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, String[] d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.item, null);
ImageView image = (ImageView) vi.findViewById(R.id.image);
imageLoader.DisplayImage(data[position], image);
return vi;
}
}
项目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="wrap_content" >
<ImageView
android:id="@+id/image"
android:layout_width="50dip"
android:layout_height="50dip"
android:scaleType="centerCrop"
android:src="@drawable/img" />
</LinearLayout>
答案 0 :(得分:0)
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="15dp"
android:layout_marginTop="25dp"
android:cacheColorHint="@android:color/transparent"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="3"
android:stackFromBottom="false"
android:stretchMode="none" >
OR
机器人:stretchMode = “spacingWidthUniform”
OR
适配器中的试试这个
<ImageView
android:id="@+id/galler_imageview"
android:layout_width="80dp"
android:layout_height="80dp"
android:contentDescription="@string/image" />
答案 1 :(得分:0)
试试这个
<?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="wrap_content" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img" />
</LinearLayout>
<GridView android:id="@+id/PhoneImageGrid"
android:layout_width="match_parent" android:layout_height="match_parent"
android:numColumns="auto_fit" android:verticalSpacing="10dp"
android:horizontalSpacing="10dp" android:columnWidth="90dp"
android:stretchMode="columnWidth" android:gravity="center"
/>
答案 2 :(得分:0)
试试这个:
<?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="wrap_content" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img" />
</LinearLayout>
<GridView android:id="@+id/PhoneImageGrid"
android:layout_width="match_parent" android:layout_height="match_parent"
android:numColumns="auto_fit" android:verticalSpacing="10dp"
android:horizontalSpacing="10dp" android:columnWidth="90dp"
android:stretchMode="columnWidth" android:gravity="center"
/>
<?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="wrap_content" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/img" />
</LinearLayout>