简体   繁体   中英

ANDROID STUDIO- layout background (image) doesn't show

everyone, first of all, I'm with a problem in my android project. I set an image as a layout background with the appropriate width (5.5), and when I run the application it appears white background instead of the intended image. The image is in at the drawable folder, all normal.

The image has the same dimensions of the layout (5.5)

Code:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:animateLayoutChanges="true" android:background="@drawable/fundo" android:orientation="vertical" tools:context=".MainActivity"> <ImageView android:id="@+id/logo" android:layout_width="wrap_content" android:layout_height="300dp" android:layout_marginTop="200dp" app:srcCompat="@drawable/logo_branco" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="450dp" android:fontFamily="@font/advent_pro_thin" android:text="O automóvel ideal para si:" android:textColor="#E76C00" android:textSize="20sp" android:textStyle="bold" /> </RelativeLayout>

If you want to load a whole image, I'd use the approach of having an ImageView at the root ViewGroup of the layout with both dimensions set to match_parent .

This will solve the issue of the image not loading with an added benefit of being able to define scale type (like centerCrop ) which enables you to have a picture with any side ratio and be sure it'll fill the screen both vertically and horizontally.

Edit:

Proposed solution:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:animateLayoutChanges="true"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/fundo" />

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:layout_marginTop="200dp"
        app:srcCompat="@drawable/logo_branco" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="450dp"
        android:fontFamily="@font/advent_pro_thin"
        android:text="O automóvel ideal para si!"
        android:textColor="#E76C00"
        android:textSize="20sp"
        android:textStyle="bold" />

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM