android:如何使子视图与父视图重叠?

发布于 2024-10-25 02:14:55 字数 153 浏览 1 评论 0原文

我需要实现如图所示的布局。 Parent 和 Sibling 位于垂直 LinearLayout 中。所以我需要创建一个子视图来重叠它的父视图。我可以在安卓上这样做吗?

布局

I need to implement the layout as in the picture. Parent and Sibling are in a vertical LinearLayout. So I need to make a child view to overlap it's parent. Can I do that in android?

layout

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

挽梦忆笙歌 2024-11-01 02:14:56

如果您使用RelativeLayout,那么实现这种效果应该没有问题。默认情况下,如果您不为所有子级提供 android:layout 参数,它将在左上角将其所有子级堆叠在一起。所以它肯定会支持重叠的孩子。您只需要找出告诉它孩子应该在屏幕上相对于其他东西的位置的最佳方式是什么。

If you use a RelativeLayout you should have no problem achieving this effect. By default it will stack all of its children on top of each other in the top left corner if you don't supply them with android:layout parameters. So it will definitely support overlapping children. You'd just have to figure out what the best way to tell it where the child should go on the screen relative to something else.

再可℃爱ぅ一点好了 2024-11-01 02:14:56

至少有两种布局可以做到这一点。绝对布局和相对布局。我建议您将视图放在相对布局中,并使用 LayoutParams 添加它们,指定它们从父级顶部和左侧的偏移量:

RelativeLayout.LayoutParams rlp;
label = new TextView(ctx);
label.setBackgroundColor(0x00000000);
label.setTextColor(0xFF7ea6cf);
label.setTextSize(13);
label.setGravity(Gravity.LEFT);
label.setText("Examples:\n- Fentanyl\n- Dilaudid 2 mg PO q 4 hours prn moderate pain");
rlp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,100);
rlp.topMargin=189;
rlp.leftMargin=30;
rlp.rightMargin=30;
rlParent.addView(label,rlp);

There are at least two layouts that can do that. AbsoluteLayout and RelativeLayout. I suggest that you put your views in a RelativeLayout and add them with LayoutParams that specify their offset form the top and left of the parent:

RelativeLayout.LayoutParams rlp;
label = new TextView(ctx);
label.setBackgroundColor(0x00000000);
label.setTextColor(0xFF7ea6cf);
label.setTextSize(13);
label.setGravity(Gravity.LEFT);
label.setText("Examples:\n- Fentanyl\n- Dilaudid 2 mg PO q 4 hours prn moderate pain");
rlp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,100);
rlp.topMargin=189;
rlp.leftMargin=30;
rlp.rightMargin=30;
rlParent.addView(label,rlp);
南街女流氓 2024-11-01 02:14:55

如果:

  1. siblingparent的兄弟
  2. parent是一个ViewGroup
  3. 并且您确实想要child 成为 parent 的孩子

,那么也许你可以考虑使用 android:clipChildren 在父级上设置为 false。

If:

  1. sibling is a sibling of parent
  2. parent is a ViewGroup
  3. and you really want child to be a child of parent

then maybe you could consider using android:clipChildren set to false on parent.

笑脸一如从前 2024-11-01 02:14:55

就我而言,我必须在父级的父级上将 android:clipChildren 设置为 false

IE

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:id="@+id/parent1">

    <FrameLayout
        android:id="@+id/parent2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="64dp"
        android:background="@android:color/holo_blue_bright">

        <View
            android:id="@+id/This_is_the_view_I_want_to_overlap_parent2"
            android:layout_width="160dp"
            android:layout_height="80dp"
            android:layout_gravity="top|start"
            android:layout_marginTop="-40dp"
            android:background="#000000" />

    </FrameLayout>


</FrameLayout>

In my case, I have to set android:clipCildren to be false on the parent of parent.

i.e.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:id="@+id/parent1">

    <FrameLayout
        android:id="@+id/parent2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="64dp"
        android:background="@android:color/holo_blue_bright">

        <View
            android:id="@+id/This_is_the_view_I_want_to_overlap_parent2"
            android:layout_width="160dp"
            android:layout_height="80dp"
            android:layout_gravity="top|start"
            android:layout_marginTop="-40dp"
            android:background="#000000" />

    </FrameLayout>


</FrameLayout>
空心↖ 2024-11-01 02:14:55

我实际上只是在看一个 FrameLayout 的示例,其中 TextView 覆盖在 ImageView 之上。因此,显然有多种方法可以完成它。你的下一个问题可能是哪一个最好......对此我不知道,但这里有一个人可能:

http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part- 1/

I was actually just looking at an example of a FrameLayout that had a TextView overlaid on top of an ImageView. So, there are obviously multiple ways to get it done. Your next question might be which one is best ... to that I have no idea, but here's a guy that might:

http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part-1/

注定孤独终老 2024-11-01 02:14:55

只需将它们全部包含在relativelayout中,并记住绘制顺序是从上到下,因此将最上面的视图放在XML定义的底部。

Just contain them all within a RelativeLayout, and remember the draw order is top to bottom, so put the top most view on the bottom of the XML definition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文