android– 自定义ListView,其中所有图像具有相同的大小

android– 自定义ListView,其中所有图像具有相同的大小,第1张

概述我想创建一个ListView,其中每一行都有一个矩形图像和一些文本.像这样:我的问题是某些图像的高度高于宽度,然后ListView看起来像这样:这是我的实际代码:xml布局<LinearLayoutxmlns:android="http://schemas.android.com/apkes/android"android:orientation="horizontal"

我想创建一个ListVIEw,其中每一行都有一个矩形图像和一些文本.像这样:

我的问题是某些图像的高度高于宽度,然后ListVIEw看起来像这样:

这是我的实际代码:

xml布局

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:orIEntation="horizontal"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent"    androID:padding="5dp"    androID:ID="@+ID/resultItem"><ImageVIEw    androID:ID="@+ID/spicture"    androID:layout_wIDth="97dp"    androID:layout_height="60dp"    androID:layout_marginBottom="3dp"    androID:layout_marginleft="5dp"    androID:layout_marginRight="10dp"    androID:adjustVIEwBounds="true"    androID:layout_margintop="3dp" ></ImageVIEw><TextVIEw        androID:ID="@+ID/sname"                androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content" /></linearLayout>

自定义适配器如下所示:

public class ListVIEwAdapter extends BaseAdapter {    private final Context mContext;    private ArrayList<Integer> imageID;    private ArrayList<String> textStr;    private final LayoutInflater inflater;    private static class VIEwHolder {        ImageVIEw imgVIEw;        TextVIEw txtVIEw;    }    public ListVIEwAdapter(Context c) {        mContext = c;        inflater = LayoutInflater.from(mContext);    }    @OverrIDe    public int getCount() {       return imageID.size();    }    @OverrIDe    public Object getItem(int position) {       return textStr.get(position);    }    @OverrIDe    public long getItemID(int position) {       return position;    }    /**     * Rep per parametre un array amb els IDentificadors de les imatges i els     * string dels textes a mostrar     *     * @param IDImages     * @param strText     */     public voID setData(ArrayList<String> strText, ArrayList<Integer> IDImages) {        imageID = IDImages;        textStr = strText;    }    @OverrIDe    public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {        VIEwHolder holder;        if (convertVIEw == null) {            holder = new VIEwHolder();            convertVIEw = inflater.inflate(R.layout.result_item, null);            holder.imgVIEw = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.spicture);            holder.txtVIEw = (TextVIEw) convertVIEw.findVIEwByID(R.ID.sname);            convertVIEw.setTag(holder);        } else {            holder = (VIEwHolder) convertVIEw.getTag();        }        Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromresource(mContext.getResources(), imageID.get(position), 50, 50);        holder.imgVIEw.setimageBitmap(bitmap);        holder.txtVIEw.setText(textStr.get(position));        holder.txtVIEw.setGravity(Gravity.CENTER);        return convertVIEw;    }}

如何将所有图像的矩形尺寸相同?

解决方法:

您可以在设置为ListVIEwItem之前裁剪图像.或者您只使用相同比例的图像.

但我认为你正在寻找一个更简单的解决方案.只需添加scaleType =“centerCrop”

<ImageVIEw    androID:ID="@+ID/spicture"    androID:scaleType="centerCrop"    androID:layout_wIDth="97dp"    androID:layout_height="60dp"    androID:layout_marginBottom="3dp"    androID:layout_marginleft="5dp"    androID:layout_marginRight="10dp"    androID:adjustVIEwBounds="true"    androID:layout_margintop="3dp" >

您可以在此处找到有关scaleTypes的更多信息:http://www.techrepublic.com/article/clear-up-ambiguity-about-android-image-view-scale-types-with-this-guide/

注意fitXY会缩放它以适应,但原始比率会丢失.

总结

以上是内存溢出为你收集整理的android – 自定义ListView,其中所有图像具有相同的大小全部内容,希望文章能够帮你解决android – 自定义ListView,其中所有图像具有相同的大小所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1107903.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存