我在ListView上工作,每行有两个方形的imageViews。 drawable由url动态给出。问题是图片不适合屏幕。
来自网址的图片已经是正方形,但我想让图片向上或向下缩放以适合屏幕。
现在它看起来像第一个截图,但我希望它们看起来像第二个。
http://dev.ts.hs.kr/jjhoon713/images/b.png
http://dev.ts.hs.kr/jjhoon713/images/a.png
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
>
<ImageView
android:id="@+id/list_item_image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
<ImageView
android:id="@+id/list_item_image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
</LinearLayout>
这是适配器
public class ListAdapter extends ArrayAdapter<ListItem>{
private ArrayList<ListItem> items;
private Activity context;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public ListAdapter(Activity context, int textViewResourceId, ArrayList<ListItem> items)
{
super(context, textViewResourceId, items);
this.items = items;
this.context = context;
inflater = (LayoutInflater)context.getLayoutInflater();
imageLoader = new ImageLoader(context);
}
public static class ViewHolder{
public ImageView image1;
public ImageView image2;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
View rowView = convertView;
ViewHolder holder;
if (rowView == null) {
rowView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.image1 = (ImageView)rowView.findViewById(R.id.list_item_image1);
holder.image2 = (ImageView)rowView.findViewById(R.id.list_item_image2);
rowView.setTag(holder);
}
else
holder = (ViewHolder)rowView.getTag();
imageLoader.DisplayImage("http://dev.ts.hs.kr/jjhoon713/images/1400023976194990.jpg", holder.image1);
imageLoader.DisplayImage("http://dev.ts.hs.kr/jjhoon713/images/1400023976194990.jpg", holder.image2);
return rowView;
}
}
答案 0 :(得分:2)
尝试这样,
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/list_item_image1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitXY"
/>
<ImageView
android:id="@+id/list_item_image2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitXY"
/>
</LinearLayout>
答案 1 :(得分:1)
尝试使用以下代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_margin="5dp"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2"
>
<ImageView
android:id="@+id/list_item_image1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:scaleType="centerCrop"
/>
<ImageView
android:id="@+id/list_item_image2"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:src="@drawable/ic_launcher"
android:scaleType="centerCrop"
/>
</LinearLayout>
答案 2 :(得分:1)
采用权重= 2的线性布局,并为每个图像视图分配1个权重,如下所示:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" // you can hardcode height if your view
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:id="@+id/imageview1"
android:layout_width="0dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:layout_height="wrap_content" // can change to fill parent if you hardcode the height of your view
android:src= ""// your image if static, else set dynamically
android:scaleType="fitxy"/>
<ImageView
android:id="@+id/imageview2"
android:layout_height=""// can change to fill parent if you hardcode the height of your view
android:layout_width="0dp"
android:adjustViewBounds="true"
android:layout_weight="1"
android:src="wrap_content" // your image if static, else set dynamically
android:scaleType="fitxy"/>
</LinearLayout>
答案 3 :(得分:1)
我的问题是list_view.xml的布局宽度是换行内容。
因为列表项的父级是list_view所以权重没有用。
我将它们改为fill_parent,现在效果很好