【问题标题】:How can set a view over another view?如何将一个视图设置在另一个视图之上?
【发布时间】:2019-03-09 17:16:47
【问题描述】:

我有一个片段。当我点击一个文本视图时,我会转到另一个片段。 但是当第二个片段显示第一个片段的按钮(btn_fragmentAccount_register“)在我的片段上时。 在我的第二个片段中是一个显示产品的回收视图。但第一个片段的按钮仍然可见。我改变了主题的高度但不起作用 我现在该怎么办?

第一个片段 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()
    }

【问题讨论】:

  • android:background="@color/white" 设置为第二个片段父视图。
  • 我做了。但是没用

标签: android android-layout view android-elevation


【解决方案1】:
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"/>

【讨论】:

  • 我也是这样做的。我总是用这种方式,但这次这个按钮没有消失
  • 如果 seconfd frgment 这次调用你使按钮可见性消失了
  • 我知道。但我认为改变按钮的可见性是不合逻辑的。没有更好的方法解决这个问题吗?
  • 如果可能,请分享您的片段布局代码和 FragmentTransaction 代码
  • 我做了。请再检查一遍
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多