【问题标题】:relativeLayout.setBackground(ContextCompat.getDrawable(context, R.drawable.sample_drawable)); is crashing the app on API 19relativeLayout.setBackground(ContextCompat.getDrawable(context, R.drawable.sample_drawable));正在使 API 19 上的应用程序崩溃
【发布时间】:2020-08-19 22:17:39
【问题描述】:

我有一个简单的布局文件,我想在主容器的背景中使用可绘制对象

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

它仍然崩溃,有人可以在这里帮助我吗? 谢谢,

【问题讨论】:

  • 你的drawables中有sample_drawable?
  • sample_drawable 是向量。显示错误日志
  • 是的,它是一个vector(xml),放在drawable文件夹中
  • 你能发布完整的崩溃报告吗?

标签: android android-layout android-drawable android-relativelayout


【解决方案1】:

我为这个问题找到了一个可行的解决方案,虽然我仍然会寻找更好的答案,但目前,我在 FrameLayout 中使用 ImageView 并设置 Imageview 的 app: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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    相关资源
    最近更新 更多