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