在LinearLayout中,第一个视图在上方/重叠第二个视图

14

有没有可能让LinearLayout中的第一个视图与第二个视图重叠显示?

我想要这样布局我的视图:

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentRight="true" >

    <TextView
        android:id="@+id/firstTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrapContent" />

    <TextView
        android:id="@+id/secondTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

但是我需要我的第一个视图从布局中的firstTextView开始,覆盖在secondTextView上方。这种情况是否可行?我正在使用LinearLayout,因为我也在尝试使用边距来获取重叠效果。


你为什么想把它放在顶部?你是指直接覆盖你的 secondTextView 吗? - codeMagic
@codeMagic 我把它放在最上面,因为“我也在玩边距以获得重叠效果”。 - RileyE
你想让第二个视图在第一个视图的下方和右侧吗? - Voicu
@Voicu 没错!:) 除此之外,我还在使用其他的LienarLayout属性,所以我更倾向于不使用另一种布局方案。 - RileyE
我看到了,但为什么不使用RelativeLayout呢?默认情况下,两者都将在左上角,然后您可以从那里调整margins,如果我理解您的意思的话。 - codeMagic
显示剩余3条评论
6个回答

9

对我起作用并且可能也对你有用的方法是:

  1. 将你的两个TextView使用RelativeLayout包裹,而非LinearLayout,并设置android:clipChildren="false"。这样可以防止重叠部分被剪切。
  2. 将第二个TextView布局在第一个TextView下面。
  3. 在代码中,在第一个TextView上调用bringToFront()。默认情况下,第一个TextView会先被绘制,并在第二个TextView下方。调用bringToFront()将更改这个顺序。

因此,布局可能如下所示:

<RelativeLayout  
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:clipChildren="false">

<TextView
    android:id="@+id/firstTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#00000000"
    android:text="First View" />

<TextView
    android:id="@+id/secondTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/firstTextView"
    android:background="#00000000"
    android:layout_marginTop="-13dp"
    android:text="Second View"/>
</RelativeLayout>

并且:

TextView firstTextView = (TextView)findViewById(R.id.firstTextView);
firstTextView.bringToFront();

5
不需要使用clipChildren。如果两个视图在同一个布局中,它们可以重叠。在相对布局中,这种情况很常见。clipChildren是用于在相对布局的父级之外绘制内容。也不需要使用bringToFront,z顺序由视图在文件中定义的顺序控制。 - Gabe Sechan

8

如果你只是想将两个视图垂直重叠,那么使用这个XML代码:

<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/firstTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00000000"
        android:text="First View" />

    <TextView
        android:id="@+id/secondTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00000000"
        android:layout_marginTop="-13dp"
        android:text="Second View"/>
</LinearLayout>

enter image description here


1
第一个视图仍然在第二个下面! - RileyE
我不需要Java代码就能知道第二个视图将覆盖在第一个视图之上。我只是使用基本的LayoutInflater。 - RileyE
如果不需要透明度,可以随意更改背景颜色。前两位数字是alpha值,其余为RRGGBB。 - Voicu
是的,但你能看到如果你改变背景颜色,第二个 View 会覆盖在第一个上面。如果你将第一个 View 的背景设置为 "#DDAA00",而第二个 View 的背景设置为 "#3300DD",那么蓝色将覆盖黄色。我需要黄色在蓝色上面。 - RileyE
混淆始于使用“在上面”而不是“在前面”。在Android世界中,“在上面”指的是视图相对于另一个视图的垂直位置。不幸的是,z轴在LinearLayout中不起作用,因此您需要使用RelativeLayout或FrameLayout。一种解决方法是反转视图并将第二个视图向外推动足够的dp,以使其看起来像第一个视图,同时仍然保持在前面。 - Voicu
好的。这对我来说是不可能的,因为另一个视图是基于“wrap_content”的。那有点糟糕。我真的需要LinearLayout.LayoutParams。抱歉造成困惑。 - RileyE

4

2
安卓系统为您留下了一个入口,可以更改孩子们的绘制顺序。
  1. 定义一个名为ReverseDrawingOrderLLayout的自定义LinearLayout
  2. 启用 isChildrenDrawingOrderEnabled = true
  3. 重写 getChildDrawingOrder 方法以更改孩子的绘制顺序
  4. 将您的布局放入 ReverseDrawingOrderLLayout 中

enter image description here

    <com.example.widget.ReverseDrawingOrderLLayout

        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_marginRight="-10dp"
            android:background="@color/b_normal"
            android:layout_width="120dp"
            android:layout_height="100dp" />

        <TextView
            android:background="@color/r_normal"
            android:layout_marginTop="10dp"
            android:layout_marginRight="-10dp"
            android:layout_width="120dp"
            android:layout_height="100dp" />

        <TextView
            android:background="@color/g_normal"
            android:layout_marginTop="20dp"
            android:layout_width="120dp"
            android:layout_height="100dp" />
    </com.example.widget.ReverseDrawingOrderLLayout>

结果 结果


运行得非常好! - Astrit Veliu

0

我知道这是一个老问题,但如果有人像我今天一样再次寻找它 - 我有一个动态构建的布局,所以我实际上没有特定的xml,而是一个单独的视图,我想根据某些设置添加到我的堆栈顶部; @GabeSechan的评论让我朝着正确的方向前进,我使用了-bottom margin将第二个视图向上移动,在位置0插入了新视图

LinearLayout stack = (LinearLayout) findViewById(R.id.mystackview);//get your stack

stack.addView(getView(count), 0); //place it at the top

public View getView(){
        TextView view = menuLayout.findViewById(R.id.myview);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(size, size);
    layoutParams.setMargins(0, 0, 0, -size));//this pulls the view back up to overlap evenly
    layoutParams.setMarginEnd(0);
    layoutParams.setMarginStart(size);

    if (view.getParent() != null)
        ((ViewGroup) view.getParent()).removeView(view);
    view.setLayoutParams(layoutParams);
    view.setZ(1);//this then sets the zindex above the other layout and hey presto, almost a simple css style fix

    return view;
}

0
通过将边距值设置为“<0”,我们可以重叠视图。但是,当我们需要重叠视图时,相对布局更可取。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接