source

Android에서 다른 보기 위에 보기 배치/오버랩(z-index)

nicesource 2023. 10. 6. 21:44
반응형

Android에서 다른 보기 위에 보기 배치/오버랩(z-index)

이미지 뷰와 텍스트 뷰로 구성된 선형 레이아웃이 있고, 아래에 선형 레이아웃이 있습니다.

<LinearLayout android:orientation="horizontal" ... >
 <ImageView 
     android:id="@+id/thumbnail"
     android:layout_weight="0.8" 
     android:layout_width="0dip"
     android:layout_height="fill_parent">
 </ImageView>
 <TextView 
    android:id="@+id/description"
    android:layout_weight="0.2"
    android:layout_width="0dip"
    android:layout_height="wrap_content">
 </TextView>

일부 규칙이 누락될 수 있습니다. 이것은 레이아웃이 어떻게 보이는지에 대한 아이디어를 제공하기 위한 것입니다.이미지 뷰 위에 가로 세로 50dip의 작은 텍스트 뷰를 하나 더 원합니다. 이미지 뷰보다 z 인덱스를 더 의미했습니다. 이미지 뷰 위에 z 인덱스를 배치합니다(오버랩).

다양한 z-index(선형 레이아웃이 좋음)를 사용하여 하나의 뷰를 다른 뷰 위에 배치하는 방법을 알고 싶습니다.

이 경우 선형 레이아웃을 사용할 수는 없지만 a. 인FrameLayout: z-index 는됩니다). 예를 들어:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_drawable"
        android:scaleType="fitCenter"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:padding="5dp"
        android:text="My Label"
        />
</FrameLayout>

이 경우 TextView(텍스트 보기)는 이미지 보기의 맨 위에 이미지의 맨 아래 중앙을 따라 그려집니다.

..bringToFront():

http://developer.android.com/reference/android/view/View.html#bringToFront%28%29

상대 레이아웃은 동일한 방식으로 작동하며 상대 레이아웃의 마지막 이미지가 이깁니다.

그리기 순서 변경

부모가 뷰를 그리는 순서를 변경하는 것도 방법입니다.setChildrenDrawingOrderEnabled(true)를 호출하고 getChildrandingOrder(childCount, inti)를 재정의하여 ViewGroup에서 이 기능을 활성화할 수 있습니다.

예:

/**
 * Example Layout that changes draw order of a FrameLayout
 */
public class OrderLayout extends FrameLayout {

    private static final int[][] DRAW_ORDERS = new int[][]{
            {0, 1, 2},
            {2, 1, 0},
            {1, 2, 0}
    };

    private int currentOrder;

    public OrderLayout(Context context) {
        super(context);
        setChildrenDrawingOrderEnabled(true);
    }

    public void setDrawOrder(int order) {
        currentOrder = order;
        invalidate();
    }

    @Override
    protected int getChildDrawingOrder(int childCount, int i) {
        return DRAW_ORDERS[currentOrder][i];
    }
}

출력:

중인 OrderLayout#setDrawOrder(int)0-1-2의 경우 다음과 같은 결과가 나옵니다.

enter image description here enter image description here enter image description here

문제를 를 해서 해결했습니다.android:elevation="1dp"어떤 관점에서 다른 관점보다 그것을 원하십니까?하지만 5.0 이하로는 표시가 안 되고, 그림자가 조금 생길 것이기 때문에 받아들일 수 있다면 괜찮습니다.

그래서 가장 정확한 해결책은 @kcoppock이라고 했습니다.

합니다.view.setZ(float)API 레벨 21부터 시작합니다.자세한 정보는 여기에서 확인할 수 있습니다.

상대 레이아웃에서 이 작업을 수행합니다.

ImageView image = new ImageView(this);
image.SetZ(float z);

저한테는 효과가 있어요.

선형 레이아웃을 사용하는 방법이 있습니다.이전 요소의 marginTop을 해당 음의 값으로 설정하고, 위에 원하는 요소가 XML에서 아래에 있는 요소 다음에 있는지 확인합니다.

<linearLayout android:orientation="horizontal" ... >
<ImageView 
 android:id="@+id/thumbnail"
 android:layout_weight="0.8" 
 android:layout_width="0dip"
 android:layout_height="fill_parent"
>
</ImageView>
<TextView 
android:id="@+id/description"
android:layout_marginTop="-20dip"
android:layout_weight="0.2"
android:layout_width="0dip"
android:layout_height="wrap_content"
>
</TextView>

선형 레이아웃으로는 할 수 없는 경우 상대 레이아웃을 선택해야 합니다.

필요할 때 한 개의 보기만 앞에 표시하려면 이 기능을 사용합니다.

containerView.bringChildToFront(topView);

containerView는 정렬할 뷰의 컨테이너이고, topView는 컨테이너에서 가장 상위에 있는 뷰입니다.

여러 보기를 정렬하려면 setChildrenDrawingOrderEnabled(true)를 사용하고 위에서 언급한 대로 getChilddrawingOrder(intchildCount, inti)를 재정의합니다.

View를 프로그래밍 방식으로 추가하는 경우 다음을 사용할 수 있습니다.yourLayout.addView(view, 1);

어디에1 is the index.

상대 레이아웃에서 이 작업을 수행합니다.

예제 XML:

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/todo"
        app:srcCompat="@drawable/ic_offer_close_outline"
        app:tint="@color/colorWhite"
        android:translationZ="999dp" />

예제 자바:

ImageView image = new ImageView(this);
image.SetZ(float z);

사용하다android:transformZ. 선형 레이아웃에서도 작동합니다.

언급URL : https://stackoverflow.com/questions/4182486/placing-overlappingz-index-a-view-above-another-view-in-android

반응형