簡體   English   中英

android:如何使子視圖與父視圖重疊?

[英]android: how to make a child view overlap the parent?

我需要實現圖片中的布局。 Parent 和 Sibling 在一個垂直的 LinearLayout 中。 所以我需要制作一個子視圖來重疊它的父視圖。 我可以在安卓中這樣做嗎?

布局

如果:

  1. 兄弟姐妹父母的兄弟姐妹
  2. parent是一個ViewGroup
  3. 你真的希望孩子成為父母的孩子

那么也許您可以考慮在parent上使用android:clipChildren設置為 false。

只需將它們全部包含在一個 RelativeLayout 中,並記住繪制順序是從上到下,因此將最頂部的視圖放在 XML 定義的底部。

我實際上只是在看一個 FrameLayout 的例子,它有一個 TextView 覆蓋在 ImageView 之上。 因此,顯然有多種方法可以完成它。 你的下一個問題可能是哪個最好……對此我不知道,但有一個人可能會:

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

如果你使用 RelativeLayout 你應該沒有問題實現這個效果。 默認情況下,如果您不為它們提供 android:layout 參數,它將在左上角將所有子項堆疊在一起。 所以它肯定會支持重疊的孩子。 您只需要找出最好的方式來告訴它孩子相對於其他東西應該在屏幕上的哪個位置。

至少有兩種布局可以做到這一點。 絕對布局和相對布局。 我建議您將視圖放在 RelativeLayout 中,並使用 LayoutParams 添加它們,這些 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);

就我而言,我必須將android:clipCildren設置為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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM