首页 > 解决方案 > 如何将一个视图设置在另一个视图之上?

问题描述

我有一个片段。当我单击文本视图时,我会转到另一个片段。但是当第二个片段显示第一个片段的按钮(btn_fragmentAccount_register“)在我的片段上时。在我的第二个片段中是一个显示产品的recyclerview。但是第一个片段的按钮仍然可见。我改变了主题的高度,但没有工作应该做什么我现在做的?

第一个片段 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:id="@+id/rel_accountFragment_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_marginTop="8dp"
        android:layout_marginRight="8dp"
        android:layout_alignParentRight="true"
        android:textColor="@color/white"
        android:layout_below="@id/edt_fragmentAccount_pass"
        android:background="@drawable/shape_btn"
        android:text="ورود"
        android:id="@+id/btn_fragmentAccount_register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/txt_fragmentAccount_favorite"
        android:layout_marginRight="8dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/view_fragmentAccount_line"
        android:text="اگهی های نشان شده"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

第二个片段 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:background="@color/white"
        android:id="@+id/rv_favFragment_list"
        android:layout_marginTop="8dp"
        android:layout_below="@+id/toolbar_main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

回收站项目 xml:

<android.support.v7.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:layout_width="match_parent"
    android:layout_marginRight="8dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:id="@+id/rel_adsRow_parent"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="4dp"
    android:layout_height="wrap_content">

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

        <ImageView
            android:id="@+id/img_adsRow_icon"
            tools:src="@mipmap/ic_launcher"
            android:layout_margin="8dp"
            android:layout_alignParentLeft="true"
            android:layout_width="90dp"
            android:layout_height="90dp" />

        <TextView
            android:textColor="@color/black"
            android:maxLines="1"
            android:ellipsize="end"
            android:id="@+id/txt_adsRow_title"
            android:layout_margin="8dp"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@id/img_adsRow_icon"
            tools:text="خودروی سمند"
            android:textSize="18sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_marginRight="8dp"
            android:layout_alignParentRight="true"
            android:id="@+id/txt_adsRow_price"
            android:layout_below="@id/txt_adsRow_title"
            tools:text="1500000 تومان"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_marginRight="8dp"
            android:layout_alignParentRight="true"
            android:layout_below="@id/txt_adsRow_price"
            tools:text="لحظاتی پیش"
            android:id="@+id/txt_adsRow_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RelativeLayout>

</android.support.v7.widget.CardView>

片段交易kotlin代码:

txtFav.setOnClickListener{
        var newContext=context as AppCompatActivity
        manager=newContext.supportFragmentManager
        var transaction=manager.beginTransaction()
        var favFragment=FavFragment()
        transaction.add(R.id.rel_accountFragment_parent,favFragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

标签: androidandroid-layoutviewandroid-elevation

解决方案


Fragment fragment = new NameofFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frmFragment, fragment).commit();
<FrameLayout
    android:id="@+id/frmFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

推荐阅读