gpt4 book ai didi

android - 使 Android ListView Row 适合它包含的图像,而不是更大

转载 作者:行者123 更新时间:2023-11-29 21:33:02 24 4
gpt4 key购买 nike

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

请看下面:

enter image description here

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>

谢谢

最佳答案

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

希望这对您有所帮助。

关于android - 使 Android ListView Row 适合它包含的图像,而不是更大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18994448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com