program story

보기를 뒤로 보내는 방법은 무엇입니까?

inputbox 2021. 1. 5. 08:04
반응형

보기를 뒤로 보내는 방법은 무엇입니까? 프로그래밍 방식으로 z 순서를 제어하는 ​​방법은 무엇입니까?


보기를 뒤로 보내는 데 문제가 있습니다. Android bringToFront()에는 다른 뷰 위에 뷰를 배치하는 것과 같은 메서드가 있습니다. 이렇게 이전 이미지 아래에 뷰를 올려 놓고 싶습니다.

같은 어떤 방법이 sendToBack()bringToBack()안드로이드는. 그렇다면 아무도 나를 도울 수 있습니까?

참고 : 레이아웃에 항목을 배치하는 순서로 z 순서를 제어하고 싶지 않습니다. z 순서를 프로그래밍 방식으로 제어하고 싶습니다.

나는 전면의 뷰를 숨기고 싶지 않고 움직이는 뷰 뒤에 있기를 원합니다.


Afaik에는이를위한 기본 제공 솔루션이 없습니다. FrameLayout 또는 이와 유사한 것에서 z 순서를 수정하려는 것 같습니다.

그러나 레이아웃에 포함 된 자식 요소의 순서를 수정할 수 있다고 생각합니다. RemoveChild ... () 및 addView () 메서드는 위치 값을 가질 수 있으므로 자식 요소를 교체 할 가능성이 가장 높으며 이는 z 순서를 수정합니다. 그러나 약간 해키 솔루션 인 것 같습니다.

또는 자식 뷰의 가시성 속성을 수정하는 것을 고려해보십시오. 유사한 동작을 얻을 수 있으며 훨씬 더 깔끔 할 것입니다.

편집하다:

새로운 Android 버전 인 L 개발자 미리보기에서는 마침내 뷰의 Z 순서를 쉽게 변경할 수있는 것으로 보입니다. 구조에 대한 'Elevation'및 'TranslationZ'속성 : https://developer.android.com/preview/material/views-shadows.html


나는 이것이 다른 답변에 암시되어 있음을 알고 있지만 아무도 코드를 게시하지 않았습니다. 뷰를 처리하기위한 "도우미"함수가있는 유틸리티 클래스에서 다음을 유지합니다.

public static void sendViewToBack(final View child) {
    final ViewGroup parent = (ViewGroup)child.getParent();
    if (null != parent) {
        parent.removeView(child);
        parent.addView(child, 0);
    }
}

방법이 없습니다 sendToBack(). 그러나 bringToFront()낮은 위치의 뷰 를 호출 하면 거의 동일한 효과를 얻을 수 있습니다.


전화 bringToFront()전면에 싶어보기에, 다음 전화 invalidate()전면에서 원하는보기를 포함하여보기에 방법을. 모든 청취자에 대해 동일하게 반복하십시오.

따라서 다른 뷰의 리스너가 호출되면 이전 뷰가 무효화되고 백그라운드에있게됩니다.


다음은 View를 뒤로 보내는 데 사용하는 방법입니다 (bringToFront의 반대, 일종의 sendToBack).

    private void moveToBack(View myCurrentView) 
    {
        ViewGroup myViewGroup = ((ViewGroup) myCurrentView.getParent());
        int index = myViewGroup.indexOfChild(myCurrentView);
        for(int i = 0; i<index; i++)
        {
            myViewGroup.bringChildToFront(myViewGroup.getChildAt(i));
        }
    }

도움이 되었기를 바랍니다!


view.setZ(float)API 레벨 21부터 사용할 수 있습니다 . 여기에서 자세한 정보를 찾을 수 있습니다.


bringToBack ()처럼 잘 작동합니다.

public void moveToBack(ViewGroup viewGroup, View v) {
    int s = 1;
    for (int i = 1; i < viewGroup.getChildCount(); i++) {
        if (viewGroup.getChildAt(1) == v) {
            s = 2;
        } else {
            viewGroup.getChildAt(s).bringToFront();
        }
    }
}

XML 파일에서 순서대로 작성해야합니다.

<FrameLayout
android:id="@+id/frameLayoutXML"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
<ImageView
 android:id="@+id/imageViewXML"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatB_XML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>

이 경우 imageViewFloatingActionButton.

변경하려는 경우 프로그래밍 방식으로 수행합니다.

public class SomeActivity extends AppCompatActivity {

    //declare variables
    ImageView imageView;
    FrameLayout frameLayout;
    FloatingActionButton floatingActionButtonNew;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //Here is to call what already exists at XML file
        frameLayout=findViewById(R.id.frameLayoutXML);   
        imageView = findViewById(R.id.imageViewXML);
        //Here is to declare a new Element to add to the view, but is not at XML File
        floatingActionButtonNew=New FloatingActionButton(this);
        frameLayout.removeAllViews();  //here you remove all views.
        frameLayout.addView(imageView);//And you add again the imageView first
        floatingActionButtonNew.setLayoutParams(new 
         RelativeLayout.LayoutParams(
          RelativeLayout.LayoutParams.WRAP_CONTENT,
          RelativeLayout.LayoutParams.WRAP_CONTENT
        )); //Then you can give some format to your floatingButtonNew

        //And finally  add to the frameLayout the new View in this case the floatingButtonNew
        frameLayout.addView(floatingActionButtonNew);
    }
}

이 뷰 선언 순서에 따라 플로팅 버튼에서 다시 이미지를 얻을 수 있습니다. 따라서 첫 번째로 선언 된 뷰는 두 번째로 선언 된 뷰에서 돌아 오며, 순서를 달성하기위한 핵심 removeAllViewsFrameLayout또는 LinearLayout프로그래밍 방식으로 추가 할 때이 뷰를 다시 추가하는 것입니다.


You can also try to use functionality of ViewGroup.addViewInLayout() method which take an index, determining should the view be at the end or at the beginning of the list


I would either set it's visibility to 0 or just bring the view under it in front of it.


If you're using appCompat library, there's another way to change Z order for devices lower than 21 API level:

ViewCompat.setTranslationZ(view, zValue);

You could try doing view.setVisibility(View.INVISIBLE) or view.setVisibility(View.GONE).

UPDATE:

This shows you how to accomplish what you want to do.

ReferenceURL : https://stackoverflow.com/questions/6759036/how-to-send-view-to-back-how-to-control-the-z-order-programmatically

반응형