如何在API级别25及以下的Android中为视图充气?

时间:2019-07-18 06:58:13

标签: android android-recyclerview layout-inflater

在回收器视图中放大视图时出现错误。它适用于Oreo和Pie,但不适用于Nougat及其下方的设备。

这是我用来放大视图的代码。

View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.listing_row_item, parent, false);

我的xml文件是

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/outlined_blue_rectangle"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    android:layout_marginBottom="@dimen/row_padding"
    android:paddingBottom="@dimen/contentPadding"
    android:paddingStart="@dimen/contentPadding"
    android:paddingEnd="@dimen/contentPadding"
    android:paddingTop="@dimen/contentPadding">

    <LinearLayout
        android:id="@+id/ll_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="Name"
            android:layout_height="wrap_content"
            android:textColor="@color/blackColor"
            android:textSize="@dimen/textSizeMedium" />

        <TextView
            android:id="@+id/name"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textColor="@color/blackColor"
            android:textSize="@dimen/textSizeMedium" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_status"
        android:layout_below="@+id/ll_name"
        android:layout_marginTop="@dimen/verticalSpacing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="Loan Application Status"
            android:layout_height="wrap_content"
            android:textColor="@color/blackColor"
            android:textSize="@dimen/textSizeMedium" />

        <TextView
            android:id="@+id/status"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textColor="@color/blackColor"
            android:textSize="@dimen/textSizeMedium" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_eligibility"
        android:layout_below="@+id/ll_status"
        android:layout_marginTop="@dimen/verticalSpacing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Eligibility Status"
            android:textColor="@color/blackColor"
            android:textSize="@dimen/textSizeMedium" />

        <TextView
            android:id="@+id/preliminary_eligibility"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textColor="@color/blackColor"
            android:textSize="@dimen/textSizeMedium" />

    </LinearLayout>

    **<Button**
        android:id="@+id/check_eligibility"
        android:layout_below="@+id/ll_eligibility"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/verticalSpacing"
        android:paddingStart="@dimen/row_padding"
        android:paddingEnd="@dimen/row_padding"
        android:text="Check\nEligibility"
        android:layout_alignParentEnd="true"
        android:textAllCaps="false"
        android:background="@drawable/button_selection_selected"
        android:textColor="@color/whiteColor"
        android:textSize="@dimen/textSizeSmall" />

    <Button
        android:id="@+id/all_info"
        android:layout_below="@+id/ll_eligibility"
        android:layout_marginTop="@dimen/verticalSpacing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingStart="@dimen/row_padding"
        android:paddingEnd="@dimen/row_padding"
        android:text="All\nInfo"
        android:textAllCaps="false"
        android:background="@drawable/button_selection_selected"
        android:textColor="@color/whiteColor"
        android:textSize="@dimen/textSizeSmall"/>

</RelativeLayout>

我收到的错误消息

android.view.InflateException: Binary XML file line #88: Binary XML file line #88: Error inflating class Button
    Caused by: android.view.InflateException: Binary XML file line #88: Error inflating class Button
    Caused by: java.lang.ArrayIndexOutOfBoundsException: length=31; index=464
        at android.content.res.StringBlock.get(StringBlock.java:65)
        at android.content.res.XmlBlock$Parser.getPooledString(XmlBlock.java:458)
        at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:1212)
        at android.content.res.TypedArray.getString(TypedArray.java:202)
        at android.widget.TextView.<init>(TextView.java:1100)
        at android.widget.Button.<init>(Button.java:109)
        at android.widget.Button.<init>(Button.java:105)
        at androidx.appcompat.widget.AppCompatButton.<init>(AppCompatButton.java:71)
        at androidx.appcompat.widget.AppCompatButton.<init>(AppCompatButton.java:67)

1 个答案:

答案 0 :(得分:1)

如果您使用的布局仅支持API级别25+,则应将其放在res/layout-v25文件夹中(如果不存在则创建文件夹),并且必须为API级别创建替代布局下方res/layout文件夹中的24。

Android将在API级别25+上自动使用layout-v25