有没有办法将视图放在其父布局之外?

And*_*lov 25 android android-layout

这是关于Android布局的问题.这是我急切想要得到的:

布局

深灰色是LinearLayout.橙色是布局X,然后绿色是FrameLayout.我需要将Green放在其父级布局X之外.所描述的布局层次结构不能更改.唯一的选择是布局X - 它可以是你想要的任何东西.

有任何想法吗?

Dar*_*pan 56

使用 -

android:clipChildren="false"
android:clipToPadding="false"
Run Code Online (Sandbox Code Playgroud)

在子视图的每个父视图中,您将视图的"左边距"设置为负视图,这会将您的子视图置于父视图之外,甚至不会剪切视图.

  • 美丽,像魅力一样......你需要将这些属性放在视图的每个父节点上. (4认同)
  • 负利润是赢家! (3认同)
  • 发现!干杯 (2认同)
  • @Denny - 不幸的是,根据我的经验和其他阅读材料,剪辑的工作方式似乎因 android 版本而异(为什么?我不知道。也许有一天 Google 会让事情始终如一地工作)所以它可能并不总是正在寻找的解决方案为了。 (2认同)

Cha*_*Yao 4

您不能将绿色部分放入布局 X 中,因为它无法在其父级之外绘制。

因此,您应该实现relativelayout或framelayout(根布局)作为所有这些视图的父级。

并将绿色视图作为根布局的子视图。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/layout_dark_grey"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_orange"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/layout_dark_grey"
        android:orientation="vertical" >
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/layout_green"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="300dp" >
    </RelativeLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)