在gridview android中需要所有相同大小的图片

时间:2017-03-02 23:57:44

标签: android image gridview

我想在我的一个活动中显示用户在gridview中发布的所有图片。这些是我的代码,我的结果如下图所示。我在gridview(90dp,90dp)中指定了每张图片的宽度和高度,但它没有跟随。他们正在填充上传大小(横向/纵向)。即使在将gridview的高度设置为wrap_content之后,高度仍然像图片一样(大约200dp可能是b)。我该如何解决这个问题?我哪里做错了?

每张图片的模型:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="90dp"
        android:layout_height="90dp"
        app:srcCompat="@drawable/post2"
        android:id="@+id/imageView2"
        android:scaleType="centerCrop"
        android:layout_margin="0dp"/>


</RelativeLayout>

我的另一项活动中的Gridview:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:id="@+id/content_competition_user"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.golstars.www.glostars.competitionUser"
    tools:showIn="@layout/activity_competition_user">

        <GridView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnWidth="90dp"
            android:numColumns="3"
            android:verticalSpacing="8dp"
            android:stretchMode="columnWidth"
            android:horizontalSpacing="8dp"
            android:id="@+id/competitionusergrid"
            />



</ScrollView>

我的适配器:

MyUser mUser = MyUser.getmUser();
        mUser.setContext(this);
        Picasso.with(this).load(mUser.getProfilePicURL()).into(profileFAB);

        targetList = new ArrayList<>();

        targetAdapter = new GridAdapter(getApplicationContext(), targetList);

        competitionusergrid.setAdapter(targetAdapter);

        String target = "";
        target = this.getIntent().getStringExtra("LOAD_TARGET");
        System.out.println(target);

        if(target.equals("COMPETITION")){
            //if we have competition pics
            if(targetList.isEmpty()){
                ArrayList<String> aux = this.getIntent().getStringArrayListExtra("COMPETITION_PICS");
                System.out.println("target list - " + aux);
                for(int i = 0; i < aux.size(); i++){
                    targetList.add(aux.get(i));
                    targetAdapter.notifyDataSetChanged();
                }


            }

外观如何:UI

3 个答案:

答案 0 :(得分:0)

使用RecyclerView代替GridView,RecyclerView与GridView&amp;易于实施。

enter image description here

答案 1 :(得分:0)

使用GridView Setup,所有图片宽度相同&amp;高度。

答案 2 :(得分:0)

如果要使用当前代码,则可以使用以下ImageView

public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
    super(context);
}

public SquareImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}}

并且,每个项目的布局,

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/post2"
        android:id="@+id/imageView2"
        android:scaleType="centerCrop"
        android:layout_margin="0dp"/>


</RelativeLayout>