relativeLayout.setBackground (ContextCompat.getDrawable (контекст, R.drawable.sample_drawable)); сбой приложения на API 19 - PullRequest
1 голос
/ 05 мая 2020

У меня есть простой файл макета, для которого я хочу использовать рисование в фоновом режиме основного контейнера

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mlv_balance_detail_container"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    **android:background="@drawable/sample_drawable"** //this was causing crash in 4.4.2 API 19
    android:padding="@dimen/dimen_16dp">

    <TextView
        android:id="@+id/tv_policy_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:letterSpacing="0.04"
        tools:text="text"
        android:textColor="#1d252d"
        android:textSize="14sp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/tv_total_balance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_policy_text"
        android:layout_marginTop="8dp"
        android:fontFamily="sans-serif"
        android:letterSpacing="0.04"
        tools:text="some text"
        android:textColor="#1d252d"
        android:textSize="12sp"
        android:textStyle="normal" />


</RelativeLayout>

Затем я попытался установить фон из кода, используя

container.setBackground(ContextCompat.getDrawable(context, R.drawable.sample_drawable));

Он все еще вылетает, может ли кто-нибудь мне помочь? Спасибо,

1 Ответ

0 голосов
/ 05 мая 2020

Я нашел рабочее решение этой проблемы, хотя я все равно буду искать лучший ответ, но пока я использую ImageView внутри FrameLayout и устанавливаю свойство app: srcCompat для Imageview, которое обратно совместимо.

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@drawable/sample_drawable" />

        </FrameLayout>

    //rest of my layout as it is

    </RelativeLayout>

...