2017-12-02 174 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