Android项目中未显示背景

Android项目中未显示背景,android,xml,android-layout,Android,Xml,Android Layout,我正在尝试在我的Android应用程序中添加渐变作为背景。我首先在我的src目录中创建了一个drawable目录,并创建了一个gradient.xml文件 gradient.xml 我还创建了一个background.xml文件 background.xml 然后在我的activity_main.xml文件中,我添加了一个图像视图,并添加了渐变作为背景 activity_main.xml 我错过了什么?背景是 android:background=“@drawable/gradie

我正在尝试在我的Android应用程序中添加渐变作为背景。我首先在我的src目录中创建了一个drawable目录,并创建了一个gradient.xml文件

gradient.xml

我还创建了一个background.xml文件

background.xml

然后在我的activity_main.xml文件中,我添加了一个图像视图,并添加了渐变作为背景

activity_main.xml

我错过了什么?

背景是 android:background=“@drawable/gradient”


但是图像视图没有大小。如果使用background.xml作为背景,它应该会显示出来。或者指定图像视图的大小,而不是使用换行符内容使用选择器可能有助于:

gradient.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
       <shape>
          <gradient
             android:angle="90"
             android:startColor="#FFFF0000"
             android:endColor="#FF00FF00"
             android:type="linear" />
       </shape>
    </item>
</selector>

显然,我需要将梯度应用于相对值

一旦我这样做了,我就可以看到背景了。不幸的是,图像视图是一个坏主意,我不需要它。这就是最终对我有用的东西

activity_main.xml


在您的图像视图中,使用高度和宽度作为某些值。对于ex 200 dps,你有一个很好的观点。好的,所以发生的事情是,我跟随的这本书的作者不是很清楚。显然,我需要将渐变添加到
而不是我甚至不需要的
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:width="2dp" android:color="#EEEEEE" />
        <padding android:left="10dp" android:top="20dp"
            android:right="10dp" android:bottom="20dp" />
    <corners android:bottomRightRadius="7dp"
        android:bottomLeftRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp"/>
</shape>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:background="@drawable/gradient" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
       <shape>
          <gradient
             android:angle="90"
             android:startColor="#FFFF0000"
             android:endColor="#FF00FF00"
             android:type="linear" />
       </shape>
    </item>
</selector>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="@drawable/gradient">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>