Android XML 在另一个布局上插入一个布局

标签 android xml android-layout android-relativelayout android-cardview

我正在开发一个允许用户创建帖子的屏幕:

here

我希望当用户单击 plus 的卡片 View 时,会打开一个相对布局,通过回收器 View 显示各种类型的电影,并且对于具有电影类型的其他卡片 View 也是如此。我不知道如何将一个布局插入另一个布局,然后使用您自己的对象。我应该做什么,创建重叠布局还是做其他事情?我无法在我的布局上叠加另一个布局, beacuse this happens

如您所见,相关布局位于主卡片 View 下方。我怎样才能把它放在上面?

完整代码:

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

最佳答案

这个例子可以解决您的问题

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>

现在在您的 MainFragment 类中

根据您的需要,您可以使用以下代码 fragment 设置inner_layout的可见性

 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");

关于Android XML 在另一个布局上插入一个布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68383894/

相关文章:

php - 为什么 php 函数 asXML() 在响应中包含 html 标签?

android - 在 FileObserver 中创建 toast 通知

android - 错误 : Cannot run aapt

android:layout_alignParentRight ="true"不起作用

android - 如何在旧的安卓设备上支持 Material Design

android - 在包中找不到属性 'layout_constraintLeft_toLeftOf' 的资源标识符

android - 以编程方式检测 7 英寸和 10 英寸平板电脑

java - 即使应用程序被杀死,如何发送通知

java - android studio 中带有 setImageDrawable 方法的 NullPointerException

XML::XML2JSON "0"元素