简体   繁体   中英

Layouts inside CardView are overlapping

I am trying to design a card layout for my App. Currently I have created a layout as below :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E4E8F8"
    android:orientation="vertical"
    android:padding="10dp"
    android:visibility="visible"
    tools:context=".MainActivity"
    tools:visibility="visible">

    <androidx.cardview.widget.CardView

        android:id="@+id/current1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:cardBackgroundColor="#ffffe0">

        <LinearLayout
            android:id="@+id/card_head2"
            android:layout_width="374dp"
            android:layout_height="49dp"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:weightSum="3">

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="70dp"
                android:layout_height="46dp"
                android:adjustViewBounds="true"
                android:baselineAligned="false"
                android:contentDescription="@string/todo"
                app:srcCompat="@drawable/ic_map" />


            <TextView
                android:id="@+id/textView1"
                android:layout_width="51dp"
                android:layout_height="46dp"
                android:gravity="center"
                android:text="@string/map"
                android:textColor="#0F0F0F"
                android:textSize="15sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="70dp"
                android:layout_height="46dp"
                android:adjustViewBounds="true"
                android:baselineAligned="false"
                android:contentDescription="@string/todo"
                app:srcCompat="@drawable/ic_calendar" />


            <TextView
                android:id="@+id/textView2"
                android:layout_width="66dp"
                android:layout_height="46dp"
                android:gravity="center"
                android:text="@string/date"
                android:textColor="#0F0F0F"
                android:textSize="15sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="57dp"
                android:layout_height="46dp"
                android:adjustViewBounds="true"
                android:baselineAligned="false"
                android:contentDescription="@string/todo"
                app:srcCompat="@drawable/ic_watch" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="60dp"
                android:layout_height="46dp"
                android:gravity="center"
                android:text="@string/time"
                android:textColor="#0F0F0F"
                android:textSize="15sp"
                android:textStyle="bold" />

        </LinearLayout>

    </androidx.cardview.widget.CardView>


</LinearLayout>

When I try to add another horizontal linear layout below the created horizontal linear layout the newly created horizontal layout is getting overlapped over existing horizontal layout inside card.

What I am trying to do is this (I have screenshot this pic by keeping the layout below the existing layout by holding mouse button ):

在此处输入图片说明

But when I release the mouse button the layout automatically gets adjusted by itself and overlaps the existing layout as follows: 在此处输入图片说明

Please help me to create the layout.

You need a LinearLayout with an orientation of vertical as the root of your CardView , to be able to stack LinearLayouts in an vertical way.

<androidx.cardview.widget.CardView
        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:id="@+id/current1"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="200dp"
        app:cardBackgroundColor="#ffffe0">
<!--You need a root layout of your CardView-->
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <LinearLayout
            android:id="@+id/card_head2"
            android:layout_width="374dp"
            android:layout_height="49dp"
            android:layout_marginStart="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:weightSum="3">

    </LinearLayout>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</LinearLayout>
</androidx.cardview.widget.CardView>

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