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



我正在开发一个屏幕,允许用户创建一个post:

此处

我希望当用户点击加号的卡片视图时,会打开一个相对的布局,通过回收视图显示各种类型的电影,而其他带有电影类型的卡片视图也是如此。我不知道如何将一个布局插入另一个布局,然后使用自己的对象。我应该怎么做,创建一个重叠的布局还是做其他事情?我无法在我的布局上叠加另一个布局,因为会发生这种情况

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

完整代码:

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

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

最新更新