android - GridView 没有创建正方形

标签 android gridview

我想创建一个简单的 android 壁纸应用程序来练习。 所以我将图片添加到 Assets 文件夹中,我想在 gridview 中显示它们。 我创建了它,但由于某种原因,gridview 中的图片没有以正方形显示,而是以大高度格式显示。 如果有人能看到问题出在哪里,我会附上我的代码:

这是它的样子:

enter image description here

您可以看到它创建的是矩形而不是等边正方形。

主要 Activity :

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView gridView = (GridView)findViewById(R.id.gridview);
        gridView.setAdapter(new ImageAdapter(this));
    }

图像适配器:

public class ImageAdapter extends BaseAdapter {
private Context context;
private String[] list;

public ImageAdapter(Context c) {
    context = c;
    try {
        list = context.getAssets().list("imgs");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public int getCount() {return list.length;}
public Object getItem(int position) {return null;}
public long getItemId(int position) {return 0;}

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView img;
    if (convertView == null) {
        img = new ImageView(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        img.setLayoutParams(new GridView.LayoutParams(params));
        img.setScaleType(ImageView.ScaleType.CENTER);
        img.setPadding(8, 8, 8, 8);
    } else {
        img = (ImageView) convertView;
    }
    try {
        InputStream ims = context.getAssets().open("imgs/" + list[position]);
        Bitmap bitmap = BitmapFactory.decodeStream(ims);
        img.setImageBitmap(bitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return img;
 }
}

这是 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
    android:stretchMode="columnWidth"
    android:numColumns="2"/>

</android.support.constraint.ConstraintLayout>

最佳答案

@TheDragoner 您的 gridview 使用 Wrap Content 作为其布局参数,即 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

因此,网格采用实际图像的高度和宽度。 更改 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelOffset(R.dimen.dp_100), getResources().getDimensionPixelOffset(R.dimen.dp_100));

关于android - GridView 没有创建正方形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53985937/

相关文章:

android - 动态添加时不显示自定义 View

c# - ASP.NET GridView 不在数据库中保存更新

c# - 按 GridView 上的“编辑”按钮时如何打开另一个页面?

c# - 如何防止我的 gridview 在 C# 中创建额外的行?

Android Activity Lifecycle和Singleton以及相互引用持久化(内存泄漏)

java - android:动态创建布局并动态设置ContentView

android - SQLite在打开数据库时强制关闭

android - 如何提高 gridview 性能 (android)

ASP.NET Gridview 分页样式中的 C# Bootstrap 分页?

android - 从 Google Play 但不是从 Android Studio 运行时出现 NullPointerException