@Dezzzigner

Почему не вставляется картинка в ImageView?

Пробую 4-я способами программно вставить картинку в ImageView, но ничего не выходит.
В чем моя ошибка или чего здесь не хватает?


Java
public class MainActivity extends AppCompatActivity {
    ImageView img1,img2,img3,img4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img1 = (ImageView) findViewById(R.id.img1);
        img2 = (ImageView) findViewById(R.id.img2);
        img3 = (ImageView) findViewById(R.id.img3);
        img4 = (ImageView) findViewById(R.id.img4);

        //setResource
//        if(img1 != null) img1.setImageResource(R.drawable.china); //Dropped

        //setDrawable
        img2.setImageDrawable(Drawable.createFromPath("/res/drawable/tokyo1.png"));

        //setBitmap
        img3.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.tokyo2));

        //Picasso(local)
        Picasso.get()
                    .load(R.drawable.tokyo1)
                    .into(img4);

        //Picasso(URI)
        Picasso.get()
                    .load("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTmG5K1tMdQ4W3G1WlZSfSWfL6jo8E9rS4NTHflXoT1CsF2Na_i")
                    .into(img1);
    }
}


XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kiril.imgtest4.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/img1"
            android:layout_width="1dp"
            android:layout_height="150dp"
            android:layout_weight="1" />
        <ImageView
            android:id="@+id/img2"
            android:layout_width="1dp"
            android:layout_height="150dp"
            android:layout_weight="1"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/img3"
            android:layout_width="1dp"
            android:layout_height="150dp"
            android:layout_weight="1"/>
        <ImageView
            android:id="@+id/img4"
            android:layout_width="1dp"
            android:layout_height="150dp"
            android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

</android.support.constraint.ConstraintLayout>
  • Вопрос задан
  • 193 просмотра
Решения вопроса 1
@Dezzzigner Автор вопроса
Перепробовав разные способы и извращения - ничего не получилось.
Вообщем, начиная с API 23 - все начинает нормально работает, и Bitmap, и Picasso с Glide.
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
zagayevskiy
@zagayevskiy Куратор тега Android
Android developer at Yandex
как страшно жить стало с тех пор, как ConstraintLayout стал корнем по-умолчанию.

Сделай layout_width = 0dp
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы