GridView没有创建正方形

时间:2018-12-31 09:44:08

标签: android gridview

我想创建一个简单的Android墙纸应用程序以供练习。 因此,我将图片添加到资源文件夹中,并希望在gridview中显示它们。 我创建了它,但是由于某种原因,gridview中的图片不是以正方形显示,而是以较大的高度格式显示。 如果有人可以看到问题所在,我将附加我的代码:

它是这样的:

enter image description here

您会看到它创建矩形而不是相等的正方形。

MainActivity:

@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));
    }

ImageAdapter:

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>

1 个答案:

答案 0 :(得分:0)

@TheDragoner您的gridview正在使用“包装内容”作为其布局参数,即2018-12-31T15:37:03.618+0530 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none' 2018-12-31T15:37:03.623+0530 I CONTROL [initandlisten] MongoDB starting : pid=xxx port=27017 dbpath=/srv/mongodb/rs0-0 64-bit host=xxx 2018-12-31T15:37:03.623+0530 I CONTROL [initandlisten] db version v4.0.5 2018-12-31T15:37:03.623+0530 I CONTROL [initandlisten] git version: xxx 2018-12-31T15:37:03.623+0530 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013 2018-12-31T15:37:03.623+0530 I CONTROL [initandlisten] allocator: tcmalloc 2018-12-31T15:37:03.623+0530 I CONTROL [initandlisten] modules: none 2018-12-31T15:37:03.623+0530 I CONTROL [initandlisten] build environment: 2018-12-31T15:37:03.623+0530 I CONTROL [initandlisten] distmod: rhel70 2018-12-31T15:37:03.623+0530 I CONTROL [initandlisten] distarch: x86_64 2018-12-31T15:37:03.623+0530 I CONTROL [initandlisten] target_arch: x86_64 2018-12-31T15:37:03.623+0530 I CONTROL [initandlisten] options: { net: { bindIp: "localhost,xxx", port: 27017 }, replication: { replSet: "rs0" }, storage: { dbPath: "/srv/mongodb/rs0-0" } } 2018-12-31T15:37:03.624+0530 I STORAGE [initandlisten] Detected data files in /srv/mongodb/rs0-0 created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'. 2018-12-31T15:37:03.625+0530 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=3399M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress), 2018-12-31T15:37:04.683+0530 I STORAGE [initandlisten] WiredTiger message [1546250824:683476][30064:0x7f4cb133db40], txn-recover: Main recovery loop: starting at 5/6144 to 6/256 2018-12-31T15:37:04.796+0530 I STORAGE [initandlisten] WiredTiger message [1546250824:796244][30064:0x7f4cb133db40], txn-recover: Recovering log 5 through 6 2018-12-31T15:37:04.871+0530 I STORAGE [initandlisten] WiredTiger message [1546250824:871037][30064:0x7f4cb133db40], txn-recover: Recovering log 6 through 6 2018-12-31T15:37:04.945+0530 I STORAGE [initandlisten] WiredTiger message [1546250824:945584][30064:0x7f4cb133db40], txn-recover: Set global recovery timestamp: 0 2018-12-31T15:37:04.964+0530 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0) 2018-12-31T15:37:04.975+0530 I CONTROL [initandlisten] 2018-12-31T15:37:04.975+0530 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2018-12-31T15:37:04.975+0530 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2018-12-31T15:37:04.975+0530 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2018-12-31T15:37:04.975+0530 I CONTROL [initandlisten] 2018-12-31T15:37:04.975+0530 I CONTROL [initandlisten] 2018-12-31T15:37:04.976+0530 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2018-12-31T15:37:04.976+0530 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2018-12-31T15:37:04.976+0530 I CONTROL [initandlisten] 2018-12-31T15:37:04.976+0530 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2018-12-31T15:37:04.976+0530 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2018-12-31T15:37:04.976+0530 I CONTROL [initandlisten] 2018-12-31T15:37:04.995+0530 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/srv/mongodb/rs0-0/diagnostic.data' 2018-12-31T15:37:04.996+0530 I REPL [initandlisten] Did not find local voted for document at startup. 2018-12-31T15:37:04.997+0530 I REPL [initandlisten] Rollback ID is 1 2018-12-31T15:37:04.997+0530 I REPL [initandlisten] Did not find local replica set configuration document at startup; NoMatchingDocument: Did not find replica set configuration document in local.system.replset 2018-12-31T15:37:04.997+0530 I CONTROL [LogicalSessionCacheRefresh] Sessions collection is not set up; waiting until next sessions refresh interval: Replication has not yet been configured 2018-12-31T15:37:04.997+0530 I NETWORK [initandlisten] waiting for connections on port 27017 2018-12-31T15:37:04.998+0530 I CONTROL [LogicalSessionCacheReap] Sessions collection is not set up; waiting until next sessions reap interval: config.system.sessions does not exist

因此,网格将获取实际图像的高度和宽度。 更改LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

相关问题