2014-11-03 53 views
1

我在一个ListView上工作,每行都有两个方形imageViews。 drawable由url动态给出。问题是图片不适合屏幕。Android。两个imageViews,完美地适合屏幕

从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

您可能需要使用[GridView控件(http://developer.android.com/guide/topics/ui/layout/gridview.html)代替。 – 2014-11-03 11:09:28

回答

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> 
+0

重量不起作用,因为图像是动态给出的。 虽然 – 2014-11-03 23:58:03

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> 
+0

谢谢我试着给他们的重量,但它不工作。无论如何感谢 ! – 2014-11-03 23:57:08

1

采取线性布局weightsum = 2,并把1个权重给每个imageview的象下面这样:

<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> 
+0

我曾尝试给重量,不工作谢谢 – 2014-11-03 23:58:27

1

我的问题是,list_view的布局宽度.xml是包装内容。

因为列表项的父母是list_view所以体重没有工作。

我改变他们FILL_PARENT,现在它工作得很好