【问题标题】:Are there any ways to put view slightly outside its parent layout?有什么方法可以将视图稍微放在其父布局之外?
【发布时间】:2012-05-03 13:27:49
【问题描述】:

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

深灰色是一种线性布局。橙色是布局 X,然后绿色是 FrameLayout。我需要将 Green 放在其父级 - 布局 X 之外。所描述的布局层次结构无法更改。唯一的选择是布局 X - 它可以是任何你想要的。

有什么想法吗?

【问题讨论】:

  • 我不想这么说,但我能想到的唯一方法是使用 FrameLayout 来包装所有内容,将深灰色和橙色放在线性布局中,然后将绿色布局放在其他布局上.这与您的“无法更改所描述的布局层次结构”相冲突。只是出于好奇,为什么不能呢?
  • 我认为您可以为边距指定负值。你试过吗?我认为它适用于可以重叠的布局(例如,画布上的两个对象)。我不在任何编辑附近,否则我会自己尝试。如果您的整个布局是画布,我肯定知道您可能会有重叠,但我不确定您的情况。
  • @Barak:它基本上是一个表格(表格行在图片上),在某些情况下,我需要将单个单元格移到其正常位置之外,所以 Table/TableRow 布局是我唯一可以使用的。
  • @Gophermofur:已经试过了,还是不行。子视图要么被裁剪,要么填充整个父视图(取决于父视图的 android:clipChildren)
  • 是的,我想会是这样。不幸的是,看起来绿色部分需要成为父容器(深灰色)的一部分,但 LinearLayout 无法满足您的需求。

标签: android android-layout


【解决方案1】:

使用 -

android:clipChildren="false"
android:clipToPadding="false"

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

【讨论】:

  • 负边距是赢家!
  • 看准了!干杯
  • @Denny - 不幸的是,根据我的经验和其他阅读材料,clip 的工作方式似乎因 android 版本不同而有所不同(为什么?我不知道。也许 Google 有一天会让事情保持一致)所以它可能并不总是正在寻找的解决方案。
  • 漂亮,就像魅力一样。您需要将这些属性放在视图的每个父级上。
  • 这应该是正确的答案,因为它完成了 OP 正在寻找的东西。
【解决方案2】:

你不能把绿色部分放在布局 X 中,因为它不能在它的父级之外绘制。

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

并将绿色View作为根Layout的childView。

<?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>

【讨论】:

  • 我知道有一些方法可以通过使用更合适的布局而不是我使用的布局来实现。我仍然很好奇在我的情况下是否有办法做到这一点。
【解决方案3】:

只需将android:clipChildren="false" 添加到父布局即可。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:clipChildren="false">

    <LinearLayout
        android:layout_marginLeft="-25dp"
        android:background="@color/colorGray"
        android:layout_width="100dp"
        android:layout_height="100dp"/>
    <LinearLayout
        android:gravity="center|left"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:background="@color/colorOrange">
        <LinearLayout
            android:layout_marginLeft="-25dp"
            android:background="@color/colorGreen"
            android:layout_width="50dp"
            android:layout_height="50dp"></LinearLayout>
    </LinearLayout>
</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-22
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多