如何在 android 中将一个视图覆盖在另一个视图上?

How to overlay a view over another view in android?

我正在尝试让一个视图覆盖另一个视图。这是我的 XML:

 <RelativeLayout
    android:background="@android:color/black"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="1"
    android:id="@+id/test"
    android:clipChildren="false"
    android:scrollbarStyle="outsideOverlay" >

    <RadioGroup
        android:id="@+id/radioButtonGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal"
        android:paddingTop="?android:attr/actionBarSize">

        <RadioButton
            android:button="@null"
            android:checked="true"
            android:contentDescription="ZONA"
            android:background="@drawable/tab_back_selector"
            android:textColor="@color/tab_text_color"
            android:textSize="12sp"
            android:layout_weight="1"
            android:id="@+id/tabZoneHome"
            android:padding="14dp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/home_tab_zone"
            android:gravity="center"/>


        <RadioButton
            android:button="@null"
            android:contentDescription="VISITADAS"
            android:background="@drawable/tab_back_selector"
            android:textColor="@color/tab_text_color"
            android:textSize="12sp"
            android:layout_weight="1"
            android:id="@+id/tabVisitedHome"
            android:padding="14dp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/home_tab_visited"
            android:gravity="center"
            android:checked="false" />

        <RadioButton
            android:button="@null"
            android:contentDescription="SUGERENCIAS"
            android:background="@drawable/tab_back_selector"
            android:textColor="@color/tab_text_color"
            android:textSize="12sp"
            android:layout_weight="1"
            android:id="@+id/tabSuggestionsHome"
            android:padding="14dp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/home_tab_suggestions"
            android:gravity="center"
            android:checked="false" />

    </RadioGroup>


    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/radioButtonGroup" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="6dp"
        android:background="@drawable/quickview_top_gradient"/>


    <ImageView
        android:id="@+id/img"           
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"            
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"    />


</RelativeLayout>

我希望 RadioGroup 高于 frame_container 但到目前为止还没有成功。我在 java 中尝试了以下操作:

  RadioGroup myButton = (RadioGroup) ((Activity) context).findViewById(R.id.radioButtonGroup);
  RelativeLayout layout = (RelativeLayout)((Activity)context).findViewById(R.id.test);
  layout.bringChildToFront(AdsRecycler.this);
  layout.invalidate();

不过什么都没有改变。 Radio Group 不覆盖 frame_container。我做错了什么?

相对布局根据其他项目的位置排列项目,据我所知,不允许重叠。请改用框架布局。

您没有正确使用框架布局。将单选组和图像视图作为框架布局的子项,您可以删除不需要的相关布局

第一个,当您指定时,您如何期望视图彼此重叠:

android:layout_below="@+id/radioButtonGroup"

这将始终移动单选按钮下方的 FrameLayout。删除此行。

其次 android 整理项目。所以你的单选按钮是基础层,然后是 FrameLayout 等等。你想先放背景(FrameLayout),然后是 RadioGroup:

<RelativeLayout>
    <FrameLayout/>

    <RadioGroup>
        ...
    </RadioGroup>

    ...
</RelativeLayout>

只需将 RadioGroup 移到 XML 中的 FrameLayout 下面即可。

<RelativeLayout
    android:background="@android:color/black"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="1"
    android:id="@+id/test"
    android:clipChildren="false"
    android:scrollbarStyle="outsideOverlay" >

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/radioButtonGroup" />

    <RadioGroup
        android:id="@+id/radioButtonGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal"
        android:paddingTop="?android:attr/actionBarSize">

        <RadioButton
            android:button="@null"
            android:checked="true"
            android:contentDescription="ZONA"
            android:background="@drawable/tab_back_selector"
            android:textColor="@color/tab_text_color"
            android:textSize="12sp"
            android:layout_weight="1"
            android:id="@+id/tabZoneHome"
            android:padding="14dp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/home_tab_zone"
            android:gravity="center"/>


        <RadioButton
            android:button="@null"
            android:contentDescription="VISITADAS"
            android:background="@drawable/tab_back_selector"
            android:textColor="@color/tab_text_color"
            android:textSize="12sp"
            android:layout_weight="1"
            android:id="@+id/tabVisitedHome"
            android:padding="14dp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/home_tab_visited"
            android:gravity="center"
            android:checked="false" />

        <RadioButton
            android:button="@null"
            android:contentDescription="SUGERENCIAS"
            android:background="@drawable/tab_back_selector"
            android:textColor="@color/tab_text_color"
            android:textSize="12sp"
            android:layout_weight="1"
            android:id="@+id/tabSuggestionsHome"
            android:padding="14dp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/home_tab_suggestions"
            android:gravity="center"
            android:checked="false" />

    </RadioGroup>


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="6dp"
        android:background="@drawable/quickview_top_gradient"/>


    <ImageView
        android:id="@+id/img"           
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"            
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"    />


</RelativeLayout>