relativeLayout.setBackground(ContextCompat.getDrawable(conte



我有一个简单的布局文件,我想在主容器的后台使用一个drawable

<?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));

它还在崩溃,有人能帮我吗?谢谢,

我找到了这个问题的有效解决方案,尽管我仍然会寻找更好的答案,但目前,我正在FrameLayout中使用ImageView,并设置应用程序:ImageView的srcCompat属性,它向后兼容。

<?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>

最新更新