2017-12-02 195 views
2

如何让我的背景图像填满整个屏幕?我尝试了几个不同的建议,似乎没有任何工作。我的应用背景不会填满屏幕

enter image description here

下面是我activity_main.xml中的代码:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 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="com.andrewvanpeter.upandaway.MainActivity"> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/background_flat_720x1280" /> 

</android.support.constraint.ConstraintLayout> 

回答

1

解决方案一:

你可以把layout_width和你ImageViewmatch_parentlayout_height

<ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:srcCompat="@drawable/background_flat_720x1280" /> 

解决方法二:

您可以在ConstraintLayout直接设置背景(不要忘记删除ImageView):

<android.support.constraint.ConstraintLayout 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" 
    android:background="@drawable/background_flat_720x1280" 
    tools:context="com.andrewvanpeter.upandaway.MainActivity"> 
+1

match_parent解决方案无法正常工作;也许我在做别的事情。但是,直接设置背景确实奏效。非常感谢你! – CheetahBongos

+0

没问题,很高兴!也许你在某处有填充或边距...或者你必须适合ImageView。 –

1

您还可以设置的ImageView的scaletype到fitXY这样的:

<ImageView 
    android:id="@+id/imageView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:scaleType="fitXY" 
    app:srcCompat="@drawable/background_flat_720x1280" /> 
+0

工作。谢谢 – CheetahBongos