首页 > 解决方案 > 我不能将放置在可绘制文件夹中的 .xml 文件用于我的应用程序的背景

问题描述

我想在我的应用程序中设计一个信息表单,但是当我使用我的自定义背景时,应用程序崩溃了。有人可以帮我解决它。

这是我的 .xml 代码

<?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"
    tools:context=".MainActivity"
    android:layout_margin="10dp"
    android:gravity="center"
    android:background="@drawable/main_activity_background" >


    <LinearLayout
        android:id="@+id/edittext_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="7"
            android:hint="User Name"
            android:textAllCaps="false" />
       </LinearLayout>

    <androidx.appcompat.widget.AppCompatButton
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:text="Submit"
        android:textAllCaps="false"
        android:textSize="17sp"
        android:background="@drawable/button_color"
        android:textColor="@color/white"
        android:layout_marginTop="10dp"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:layout_below="@+id/devide_line" />

</RelativeLayout>

这是XML与drawable相关的文件代码

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item>

        <shape>

            <gradient

                android:endColor="#FAEBD7"
                android:angle="180"
                android:type="radial"/>

            <corners
                android:radius="300dp" />

        </shape>

    </item>

</selector>

标签: javaandroidxmldrawable

解决方案


将您的可绘制 xml 代码包装在<layer-list>标签中

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item>
    
        <shape>
    
            <gradient
    
                android:endColor="#FAEBD7"
                android:angle="180"
                android:type="radial"
    
                />
    
            <corners
    
                android:radius="300dp"
    
                />
    
        </shape>
    
    </item>

<layer-list>

推荐阅读