1

我有一个 Android 列表,我想让“ListView”的行尺寸与其中的实际图像相匹配。目前该行大于其中的图像,因此扭曲了列表。

请看下面:

在此处输入图像描述

My activity (ActivitytToyotaOwnersMainListView.java, related code only):


public class ActivitytToyotaOwnersMainListView extends Activity {


    public ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_toyota_owners_main);

        ListViewItemModelLoader.LoadModel();

        listView = (ListView) findViewById(R.id.toyota_owners_main_list_view);

        if (listView!=null)
        {

        String[] ids = new String[ListViewItemModelLoader.Items.size()];
        for (int i= 0; i < ids.length; i++){

            ids[i] = Integer.toString(i+1);
        }

        ItemAdapter adapter = new ItemAdapter(this,R.layout.main_activity_view_icons, ids);
        listView.setAdapter(adapter);
        }
        else
        {   Log.d("***Class ActivitytToyotaOwnersMainListView, listView: ",
                String.valueOf(listView));

        }


        // OnItemClickListener
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
......

ActivitytToyotaOwnersMainListView 布局 xml (activity_toyota_owners_main.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ToyotaOwnersMainActivity" >

   <ListView
       android:id="@+id/toyota_owners_main_list_view"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true" >
   </ListView>

</RelativeLayout>

My ItemAdapter(用于在列表中显示图像的自定义适配器)

package com.toyota.toyotaownerspoc.main;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.AbsListView.LayoutParams;
import java.io.InputStream;

import com.toyota.toyotaownerspoc.R;

public class ItemAdapter extends ArrayAdapter<String> {

    private final Context context;
    private final String[] Ids;
    private final int rowResourceId;

    public ItemAdapter(Context context, int textViewResourceId, String[] objects) {

        super(context, textViewResourceId, objects);

        this.context = context;
        this.Ids = objects;
        this.rowResourceId = textViewResourceId;

    }

    @SuppressWarnings("finally")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View rowView = null;
        // get input stream
        InputStream ims = null;
        try {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (inflater != null) {
                rowView = inflater.inflate(rowResourceId, parent, false);
                if (rowView != null) {
                    ImageView imageView = (ImageView) rowView
                            .findViewById(R.id.main_activity_icons_image_view);

                    if (imageView != null) {
                        int id = Integer.parseInt(Ids[position]);
                        String resourceId = ListViewItemModelLoader.GetbyId(id).resourceFile;

                        // load image as Drawable
                        int resource = context.getResources().getIdentifier(
                                resourceId, "drawable",
                                context.getPackageName());
                        Drawable drawable = context.getResources().getDrawable(
                                resource);

                        // set image to ImageView
                        imageView.setImageDrawable(drawable);

                    } else {
                        Log.d("***Class ItemAdapter , imageView: ",
                                String.valueOf(imageView));

                    }
                } else {
                    Log.d("***Class ItemAdapter , rowView: ",
                            String.valueOf(rowView));
                }
            } else {
                Log.d("***Class ItemAdapter , inflater: ",
                        String.valueOf(inflater));
            }
        } catch (Exception exc) {
            Log.d("***Class ItemAdapter , getView(int position, View convertView, ViewGroup parent): ",
                    exc.getMessage());
        } finally {

            return rowView;
        }

    }

}

将在每个 ListView 行内的图像布局 (main_activity_view_icons.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:minHeight="64dp">
<ImageView 
    android:id="@+id/main_activity_icons_image_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

   </ImageView>

</RelativeLayout>

谢谢

4

4 回答 4

2

我注意到行布局的 RelatiiveLayout 中的 minHeight 参数。尝试删除它。另外,尝试设置相对布局的布局宽度/高度以及那里的图像视图以“包装内容”

希望这可以帮助。

于 2013-09-25T00:59:47.933 回答
0

利用..

android:scaleType="fitXY"

在您的 ImageView 中删除多余的空间

或者

您可以缩放图像以适合您的屏幕,因为您的图像大于屏幕宽度..

于 2013-11-18T12:12:57.987 回答
0

你可以用这个

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:minHeight="64dp">
<ImageView 
    android:id="@+id/main_activity_icons_image_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</ImageView>

</RelativeLayout>
于 2013-09-25T02:02:42.607 回答
0

我遇到了类似的问题,最终过于明确地指定了 ImageView 的高度。基本上 ImageView layout_height 应该指定与布局 minHeight 属性中的行高相同的值。在您的情况下,如下所示:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:minHeight="64dp">
    <ImageView 
        android:id="@+id/main_activity_icons_image_view"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:scaleType="fitCenter">        
   </ImageView>
</RelativeLayout>

从 scaleType 到 fitCenter 并不是我自己做的。

于 2017-03-22T11:25:37.287 回答