ListView所有图像具有相同的大小

时间:2017-11-07 19:43:10

标签: android image listview imageview

如何让这些照片保持相同的尺寸?

我尝试了'pod 'mylibrary', :git => 'https://s3.amazonaws.com/myBucketThatHostsGitRepo'scaleType,但都没有。

请看下面的图片

Images with diferent sizes

自定义ListView

ajustbounds

列表视图

    <ImageView
    android:id="@+id/imageView13"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:scaleType="centerCrop"
    android:layout_alignParentStart="true"/>

2 个答案:

答案 0 :(得分:0)

实现此目的的一种方法是通过自定义视图控制纵横比。像这样的东西

MyWidget.include({
    init: function(parent, options) {
        this.events["click .new_button"] = "new_button_clicked";
        this._super(parent, options);
    },

    new_button_clicked: function() {
        console.log("New Button Clicked.");
    } 
});

然后在XML中使用自定义ImageView

/**
 * Created by victoraldir on 11/05/2017.
 */

import android.content.Context;
import android.util.AttributeSet;

public class DynamicImageView extends android.support.v7.widget.AppCompatImageView {

    private static final float ASPECT_RATIO = 1.5f;

    public DynamicImageView(Context context) {
        super(context);
    }

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

    public DynamicImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = Math.round(width * ASPECT_RATIO);
        setMeasuredDimension(width, height);
    }
}

答案 1 :(得分:0)

我使用自定义列表CustomList.java类和单个布局list_single.xml。试试这个。

MainActivity.java文件看起来像这样。

document.querySelectorAll('img.svg').forEach(function(element) {
            var imgID = element.getAttribute('id')
            var imgClass = element.getAttribute('class')
            var imgURL = element.getAttribute('src')

            xhr = new XMLHttpRequest()
            xhr.onreadystatechange = function() {
                if(xhr.readyState == 4 && xhr.status == 200) {
                    var svg = xhr.responseXML.getElementsByTagName('svg')[0];

                    if(imgID != null) {
                         svg.setAttribute('id', imgID);
                    }

                    if(imgClass != null) {
                         svg.setAttribute('class', imgClass + ' replaced-svg');
                    }

                    svg.removeAttribute('xmlns:a')

                    if(!svg.hasAttribute('viewBox') && svg.hasAttribute('height') && svg.hasAttribute('width')) {
                        svg.setAttribute('viewBox', '0 0 ' + svg.getAttribute('height') + ' ' + svg.getAttribute('width'))
                    }
                    element.parentElement.replaceChild(svg, element)
                }
            }
            xhr.open('GET', imgURL, true)
            xhr.send(null)
        })

CustomList.java文件看起来像这样

CustomList cl; //Create cl as global variable
String[] imagename={"image1","image2"}; //create image name array or do that dynamically
Integer[] imageId={"R.drawable.image1","R.drawable.image2"};  //create image locations array or do that dynamically
ListView list; 

//inside oncreate method
list = (ListView) getActivity().findViewById(R.id.Listview1);
cl = new CustomList(getActivity(), imagename, imageId); //initialize cl
list.setAdapter(cl); 

list_single.xml布局文件看起来像这样

public class CustomList extends ArrayAdapter<String>{
    private final Activity context;
    private final String[] imagename;
    private final Integer[] imageId;
    public CustomList(Activity context, String[] imagename, Integer[] imageId) {
        super(context, R.layout.list_single, web);
        this.context = context;
        this.imagename = imagename;
        this.imageId = imageId;
    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.list_single, null, true);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        txtTitle.setText(imagename[position]);
        imageView.setImageResource(imageId[position]);
        return rowView;
    }
}

或者您可以在imageview中设置初始宽度和高度

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

    <TableRow
        android:paddingBottom="2dp"
        android:paddingTop="2dp">

        <ImageView
            android:id="@+id/img"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@android:color/holo_green_light"
            android:paddingBottom="1dp"
            android:paddingLeft="1dp"
            android:paddingRight="1dp"
            android:paddingTop="1dp" />

        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            android:paddingLeft="5dp"
            android:textColor="@android:color/background_dark"
            android:textSize="15sp" />

    </TableRow>
</TableLayout>