简体   繁体   中英

Android XML Insert a layout on another layout

I am developing a screen that allows the user to create a post:

here

I want that when the user clicks on the cardview of the plus, a relative layout opens that through a recyclerview shows the various types of films, and the same thing for the other cardview with the genres of films. I don't know how to insert a layout into another layout and then use your own objects. What should I do, create an overlapping layout or do something else? I can't superimpose another layout on mine, beacuse this happens

As you can see the related layout is below the main cardview. How could I put it on top?

Full 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"
    tools:context=".userProfile">





    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="-37dp"
        android:backgroundTint="#EFE8AB"
        app:cardCornerRadius="40dp">

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


            <TextView
                android:id="@+id/txtVal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Valutazione"
                android:textColor="@color/black"
                android:fontFamily="@font/amiko"
                android:textSize="25dp"
                android:layout_marginTop="230dp"
                android:layout_alignStart="@+id/img_imgpost"

                />

            <androidx.cardview.widget.CardView
                android:id="@+id/imBackReg"
                android:layout_width="65dp"
                android:layout_height="68dp"
                app:cardCornerRadius="25dp"
                android:layout_marginRight="8dp"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="60dp"
                android:backgroundTint="@color/redButton2">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/ic_baseline_close_24"
                    />
            </androidx.cardview.widget.CardView>

            <TextView
                android:id="@+id/d"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Descrizione:"
                android:textColor="@color/black"
                android:fontFamily="@font/amiko"
                android:textSize="25dp"
                android:layout_marginTop="295dp"
                android:layout_alignStart="@+id/img_imgpost"

                />

            <EditText

                android:id="@+id/txt_desc"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="330dp"
                android:background="@color/primaryPopHome2"
                android:ems="10"
                android:backgroundTint="#EFE8AB"
                android:gravity="top|left"
                android:hint="Descrizione"
                android:layout_alignStart="@+id/txtVal"
                android:inputType="textMultiLine"
                android:textColor="@color/black"
                android:textColorHint="#b7b7b7"


                />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="450dp"
                android:layout_marginHorizontal="15dp"
                android:background="@color/darkpost"/>

            <HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:layout_marginTop="470dp"
                >

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/txtT"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Tipologia:"
                        android:textColor="@color/black"
                        android:fontFamily="@font/amiko"
                        android:textSize="25dp"
                        android:layout_marginLeft="2dp"
                        android:layout_centerVertical="true"
                        />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/black"
                        android:text="Tipologia scelta"
                        android:layout_marginTop="45dp"
                        android:layout_marginLeft="3dp"
                        />
                    <androidx.cardview.widget.CardView
                        android:id="@+id/chooseTip"
                        android:layout_width="65dp"
                        android:layout_height="68dp"
                        app:cardCornerRadius="20dp"
                        android:layout_marginLeft="8dp"
                        android:layout_toEndOf="@+id/txtT"
                        android:layout_centerVertical="true"
                        android:backgroundTint="@color/darkpost">

                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:src="@drawable/ic_baseline_add_24"/>

                    </androidx.cardview.widget.CardView>



                    <TextView
                        android:id="@+id/txtC"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Categoria:"
                        android:layout_centerVertical="true"
                        android:textColor="@color/black"
                        android:fontFamily="@font/amiko"
                        android:textSize="25dp"
                        android:layout_marginLeft="10dp"
                        android:layout_toEndOf="@+id/chooseTip"

                        />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/black"
                        android:text="Categoria scelta"
                        android:layout_marginTop="45dp"
                        android:layout_alignStart="@id/txtC"
                        android:layout_marginLeft="2dp"
                        />

                    <androidx.cardview.widget.CardView
                        android:id="@+id/chooseCat"
                        android:layout_width="65dp"
                        android:layout_height="68dp"
                        app:cardCornerRadius="20dp"
                        android:layout_marginLeft="8dp"
                        android:layout_centerVertical="true"
                        android:layout_toEndOf="@+id/txtC"
                        android:backgroundTint="@color/darkpost">

                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:src="@drawable/ic_baseline_add_24"/>

                    </androidx.cardview.widget.CardView>



                </RelativeLayout>

            </HorizontalScrollView>




            <EditText
                android:id="@+id/txt_valuta"
                android:layout_width="50dp"
                android:layout_height="46dp"
                android:layout_marginTop="220dp"
                android:layout_marginLeft="13dp"
                android:layout_centerHorizontal="true"
                android:background="@drawable/textview_new_style"
                android:ems="10"
                android:hint="0"
                android:text=""
                android:textSize="25dp"
                android:textAlignment="center"
                android:layout_toEndOf="@+id/txtVal"

                android:textColor="@color/black"
                android:textColorHint="#EFE8AB"

                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="/10"
                android:textSize="25dp"
                android:textColor="@color/black"
                android:layout_marginTop="225dp"
                android:layout_marginLeft="5dp"
                android:layout_toEndOf="@+id/txt_valuta"
                />



            <androidx.cardview.widget.CardView
                android:id="@+id/img_imgpost"
                android:layout_width="140dp"
                android:layout_height="150dp"
                app:cardCornerRadius="30dp"
                android:layout_marginLeft="20dp"
                android:layout_alignParentTop="true"
                android:layout_marginTop="50dp"
                android:backgroundTint="@color/darkpost">

                <ImageView
                    android:id="@+id/imgPostICV"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@color/darkpost"/>

            </androidx.cardview.widget.CardView>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Titolo:"
                android:textColor="@color/black"
                android:fontFamily="@font/amiko"
                android:textSize="25dp"
                android:layout_marginTop="567dp"
                android:layout_alignStart="@+id/img_imgpost"

                />


            <EditText
                android:id="@+id/txt_titoloo"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginTop="600dp"
                android:layout_marginHorizontal="5dp"
                android:backgroundTint="@color/darkpost"
                android:ems="10"
                android:hint="Titolo"

                android:layout_marginBottom="20dp"
                android:inputType="textEmailAddress"
                android:textColor="@color/black"
                android:textColorHint="#b7b7b7"
                android:layout_alignStart="@+id/d"/>


        </RelativeLayout>

    </androidx.cardview.widget.CardView>


    <androidx.cardview.widget.CardView
        android:id="@+id/btn_invia"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginTop="660dp"
        app:cardCornerRadius="15dp"
        android:layout_marginBottom="10dp"
        android:layout_marginHorizontal="20dp"
        android:backgroundTint="#E8E1A2"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Pubblica il Post"
                android:textColor="@color/black"
                android:layout_centerInParent="true"
                android:textSize="30dp"
                android:fontFamily="@font/amiko"
                android:textStyle="bold"/>

        </RelativeLayout>



    </androidx.cardview.widget.CardView>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"/>



</RelativeLayout>

This example could solve your problem

fragment_main.xml

<?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" >

<include
    android:id="@+id/innerLayout"
    layout="@layout/inner_layout" />

</RelativeLayout>

inner_layout.xml

<?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:id="top_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="inner text" />

</RelativeLayout>

Now in your MainFragment class

Depending on your need you can set the visibility of the inner_layout by using the following code snippet

 View innerLayout = findViewById(R.id.innerLayout);

 //For hiding the innner layout dynamically
 innerLayout.top_layout.setVisibility(View.GONE);    

 //For showing the innner layout dynamically
 innerLayout.top_layout.setVisibility(View.VISIBLE);  

//to set text in TextView
innerLayout.text.setText("Anything you want");

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