gpt4 book ai didi

java - Android:GridView倾斜显示图像

转载 作者:行者123 更新时间:2023-12-01 13:45:56 25 4
gpt4 key购买 nike

我正在尝试让 GridView 显示指定文件中的所有图像,它正在执行此操作。

然而,图像并没有出现在网格中,而是很奇怪......参见屏幕截图

screenshot

我不知道是什么导致了这个问题,无论是代码还是与图像大小有关。

任何有关代码解决方案或其他方面的帮助都会很棒

以下是 GridView 和适配器的代码

public class MyWallpapers extends Activity {


private ImageAdapter imageAdapter;

ImageView Selected;
String ImageLocation = null;


ArrayList<String> f = new ArrayList<String>();// list of file paths
File[] listFile;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper_view_activity);


getFromSdcard();
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);


imagegrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {


//Toast.makeText(MyWallpapers.this, "" + listFile[position].getAbsolutePath(), Toast.LENGTH_SHORT).show();


Intent intent = new Intent(MyWallpapers.this, WallpaperCrop.class);

intent.putExtra("FilePath",listFile[position].getAbsolutePath());
startActivity(intent);
}
});
}


public void getFromSdcard()
{
File file= new File(android.os.Environment.getExternalStorageDirectory(),"Wallpaper");

if (file.isDirectory())
{
listFile = file.listFiles();


for (int i = 0; i < listFile.length; i++)
{

f.add(listFile[i].getAbsolutePath());

}
}
}

public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;

public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(
R.layout.galleryitem, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);

convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}


Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
holder.imageview.setImageBitmap(myBitmap);
return convertView;
}
}
class ViewHolder {
ImageView imageview;


}
}

galleryitem.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" >

<ImageView
android:id="@+id/thumbImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />

</RelativeLayout>

和wallpaper_view_activity.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"
android:background="@drawable/seachpage" >

<GridView
android:id="@+id/PhoneImageGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="@drawable/bottomcurve" />

</RelativeLayout>

最佳答案

您应该在适配器的项目尺寸膨胀后手动设置它,如下所示,因为您的画廊项目有两个尺寸 fill_parent 那么它会产生问题。

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(
R.layout.galleryitem, null);

int yourExpectedHeight = 100;
convertView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT,
yourExpectedHeight));

holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);

convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}


Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
holder.imageview.setImageBitmap(myBitmap);
return convertView;
}

关于java - Android:GridView倾斜显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20377038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com