【问题标题】:Android XML Insert a layout on another layoutAndroid XML 在另一个布局上插入布局
【发布时间】:2021-09-23 18:37:47
【问题描述】:

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

here

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

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

完整代码:

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

【问题讨论】:

  • 在单个 xml 中包含这么多元素不是一个好习惯,我建议使用带有自定义视图的警报对话框,因此您可以在该视图中放置您的 rv 和其他任何东西

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


【解决方案1】:

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

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 类中

根据您的需要,您可以使用以下代码 sn-p 设置 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");

【讨论】:

  • 明天我会尝试你的解决方案,如果它解决了我的问题,我会把你作为决定性的答案。在这种情况下使用片段实际上似乎没问题。感谢回复
猜你喜欢
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
  • 2012-08-17
  • 2012-09-11
相关资源
最近更新 更多