Pregunta

I can't seem to get these image views working. I wan't to create a few imageviews dynamically but I want them to have a template or so from xml.

My loop looks as so:

LinearLayout layout = (LinearLayout) findViewById(R.id.mainLinearLayout);

    for (int i=0; i<10; i++){

        ImageView imgView = new ImageView(this);
        imgView.setAdjustViewBounds(true);
        imgView.setId(R.id.coverview1);
        layout.addView(imgView);
        Picasso.with(context).load("http://blah.com/image.jpg").into(imgView);

    }

I'm creating a few images and just placing some temporary image in them but can't seem change the image size at all.

Here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:id="@+id/mainLinearLayout">

        <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/coverview1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                android:maxWidth="20dp"
                android:layout_gravity="center_horizontal" />
        </RelativeLayout>
    </LinearLayout>

</ScrollView>

Taking the advice of the answer below, I added what he said but now am not able to change the scale type.

imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imgView.setAdjustViewBounds(true);

But the image is still the height of the container and not stretching.

¿Fue útil?

Solución

You Need to add LayoutParams for your ChildView Like:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                yourCalculatedWidth, LayoutParams.FILL_PARENT);

params.gravity=Gravity.CENTER_HORIZONTAL;

imgView.setLayoutParams(params);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top