【问题标题】:How to overlay a view over another view in android?如何将一个视图覆盖在android中的另一个视图上?
【发布时间】:2015-02-10 14:27:32
【问题描述】:

我试图让一个视图覆盖另一个视图。这是我的 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 xml view


    【解决方案1】:

    首先,当您指定时,您如何期望视图相互重叠:

    android:layout_below="@+id/radioButtonGroup"
    

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

    第二 android按顺序放置项目。所以你的单选按钮是基础层,然后是 FrameLayout 等等。你要先放背景(FrameLayout),再放RadioGroup

    <RelativeLayout>
        <FrameLayout/>
    
        <RadioGroup>
            ...
        </RadioGroup>
    
        ...
    </RelativeLayout>
    

    【讨论】:

    • 天哪……我好惭愧……当我错过这样的事情时,我讨厌这个人:(非常感谢!
    【解决方案2】:

    只需将 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>
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-07-25
          • 2011-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-04
          相关资源
          最近更新 更多