Home > Net >  How to avoid unnecessary Relative layout in Linear Layout
How to avoid unnecessary Relative layout in Linear Layout

Time:11-06

I am using a Linear Layout to display an ImageView and to make it be displayed only one half of the screen. What I have done is to add a Relative layout inside this Linear Layout so that this ImageView only occupies one half of the screen. I am getting a warning as it is normal because this Relative Layout doen't have any children but I have used it only to occupy the other half of the screen. How could I make the same result avoiding this warning?

This is what I have:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="2">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/img"
            android:scaleType="fitXY"
            tools:ignore="ContentDescription" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >

CodePudding user response:

Use below code, imageView is only half the screen:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/img"
        android:scaleType="fitXY"
        tools:ignore="ContentDescription" />

</LinearLayout>
  • Related