1

Есть кнопка (TextView) и у ней нужно заменить изображение - как это делается программно?введите сюда описание изображения

<?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="wrap_content"
android:layout_height="wrap_content">

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textView_item_currency"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_rowWeight="1"
    android:layout_margin="2dp"
    android:background="@color/colorPanel"
    android:drawableTop="@drawable/ic_question_mark"
    android:gravity="center"
    android:paddingStart="8dp"
    android:paddingTop="8dp"
    android:paddingEnd="8dp"
    android:text="@string/currency"
    android:textColor="@color/colorText"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
1
  • удалил лишние комментарии и оформил как ответ
    – Jarvis_J
    19 янв 2019 в 14:37

2 ответа 2

5

Для того, чтобы программно изменять картинку у TextView используйте:

setCompoundDrawablesWithIntrinsicBounds(null,
     getResources().getDrawable(R.drawable.ic_dogecoin),null,null)
//картинки(слева, вверху, справа, снизу)
3
button.setBackgroundResource(R.drawable.image);

Или

imageButton.setImageResource(R.drawable.image);
3
  • в первом варианте картинка растягивается на всю кнопку а второго нет у Button
    – S. Oleg
    19 янв 2019 в 14:30
  • Точно, второе у ImageButton. 19 янв 2019 в 14:35
  • Ай блин, так это даже не кнопка в вопросе, а текствью. Заголовок и метка вводят в заблуждение. 19 янв 2019 в 14:38

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.