android - Android项目中不显示背景

标签 android xml android-layout

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

enter image description here

渐变.xml

<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#0000ff"
        android:endColor="#000000"
        android:angle="270" />
</shape>

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

背景.xml

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

然后在我的 activity_main.xml 文件中,我添加了一个 ImageView 并将渐变添加为背景

activity_main.xml

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

我错过了什么?

最佳答案

背景是 android:background="@drawable/gradient"

但是 ImageView 没有尺寸。如果您使用 background.xml 作为背景,它应该会显示出来。或者指定 ImageView 的大小而不是使用 wrap_content

关于android - Android项目中不显示背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26794043/

相关文章:

android-layout - addView方法动态添加View时onDraw调用两次

java - Android中的分页文本

android - DownloadManager 在目录创建时的行为

xml - 强制 Firefox 像时尚一样在树中呈现 XML

android - Calabash - 我的设备不会旋转

java - 将 xsd 转换为 java 时已经定义的错误

xml - 如何在 MSXML6 上获得 max() 的等效项?

android - 如何在edittext中设置不可编辑的文本

android - 如何解决“"' generateDebugSources' not found in project”错误?

java - 应用发布后如何处理RuntimeException