모든 것을 한 눈에 보는 방법?
나는 활동과 많은 위젯을 가지고 있으며, 그들 중 일부는 애니메이션을 가지고 있으며 애니메이션 때문에 일부 위젯이 서로 이동 (번역)하고 있습니다. 예를 들어 텍스트보기가 일부 버튼 위로 이동하고 있습니다. . .
이제 버튼이 항상 앞에 있기를 원합니다. 그리고 텍스트 뷰가 움직일 때 버튼 뒤로 이동하고 싶습니다.
이 작업을 수행 할 수 없어 내가 아는 모든 것을 시도했지만 "bringToFront ()"가 제대로 작동하지 않습니다.
참고 내가 할 수없는 레이아웃 원인에 요소를 배치하는 순서로 z 순서를 제어하고 싶지 않습니다.) : 레이아웃이 복잡하고 레이아웃을 구걸 할 때 모든 버튼을 배치 할 수 없습니다
전면에서 가져 오려는 뷰에서 bringToFront ()를 호출 할 수 있습니다.
이것은 예입니다 :
yourView.bringToFront();
나는 좋은 대답을 찾기 위해 스택 오버플로를 살펴 보았고 찾을 수 없을 때 문서를 살펴 보았습니다.
아직이 간단한 답변을 우연히 본 사람은 없습니다.
ViewCompat.setTranslationZ(view, translationZ);
기본 번역 z는 0.0
더 간단한 해결책은 활동의 XML을 편집하는 것입니다. 사용하다
android:translationZ=""
bringToFront()
올바른 방법이지만 루트 뷰 아래의 최상위 뷰에서 다음 bringToFront()
과 같이 호출 하고 invalidate()
메서드 를 지정해야합니다 .
뷰의 계층 구조는 다음과 같습니다.
-RelativeLayout
|--LinearLayout1
|------Button1
|------Button2
|------Button3
|--ImageView
|--LinearLayout2
|------Button4
|------Button5
|------Button6
따라서 버튼을 다시 애니메이션으로 만들면 (1-> 6) 버튼이 (아래)에 나타납니다 ImageView
. 그것을 (위) 가져 오려면 ImageView
전화 bringToFront()
를 invalidate()
걸어서 메소드를 작성해야 LinearLayout
합니다. 그런 다음 작동합니다.) ** 참고 : android:clipChildren="false"
루트 레이아웃 또는 애니메이션 뷰의 gradparent_layout 을 설정 해야합니다. 내 실제 코드를 살펴 보겠습니다.
.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:hw="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/common_theme_color"
android:orientation="vertical" >
<com.binh.helloworld.customviews.HWActionBar
android:id="@+id/action_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_actionbar_height"
android:layout_alignParentTop="true"
hw:titleText="@string/app_name" >
</com.binh.helloworld.customviews.HWActionBar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/action_bar"
android:clipChildren="false" >
<LinearLayout
android:id="@+id/layout_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>
<ImageView
android:id="@+id/imgv_main"
android:layout_width="@dimen/common_imgv_height"
android:layout_height="@dimen/common_imgv_height"
android:layout_centerInParent="true"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:id="@+id/layout_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
.java의 일부 코드
private LinearLayout layoutTop, layoutBottom;
...
layoutTop = (LinearLayout) rootView.findViewById(R.id.layout_top);
layoutBottom = (LinearLayout) rootView.findViewById(R.id.layout_bottom);
...
//when animate back
//dragedView is my layoutTop's child view (i added programmatically) (like buttons in above example)
dragedView.setVisibility(View.GONE);
layoutTop.bringToFront();
layoutTop.invalidate();
dragedView.startAnimation(animation); // TranslateAnimation
dragedView.setVisibility(View.VISIBLE);
GLuck!
Try FrameLayout
, it gives you the possibility to put views one above another. You can create two LinearLayouts
: one with the background views, and one with foreground views, and combine them using the FrameLayout
. Hope this helps.
With this code in xml
android:translationZ="90dp"
If you are using ConstraintLayout
, just put the element after the other elements to make it on front than the others
There can be another way which saves the day. Just init a new Dialog with desired layout and just show it. I need it for showing a loadingView over a DialogFragment and this was the only way I succeed.
Dialog topDialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
topDialog.setContentView(R.layout.dialog_top);
topDialog.show();
bringToFront() might not work in some cases like mine. But content of dialog_top layout must override anything on the ui layer. But anyway, this is an ugly workaround.
You can try to use the bringChildToFront
, you can check if this documentation is helpful in the Android Developers page.
i have faced the same problem. the following solution have worked for me.
FrameLayout glFrame=(FrameLayout) findViewById(R.id.animatedView);
glFrame.addView(yourView);
glFrame.bringToFront();
glFrame.invalidate();
2nd solution is by using xml adding this attribute to the view xml
android:translationZ=""
You need to use framelayout. And the better way to do this is to make the view invisible when thay are not require. Also you need to set the position for each and every view,So that they will move according to there corresponding position
If you are using a LinearLayout
you should call myView.bringToFront()
and after you should call parentView.requestLayout()
and parentView.invalidate()
to force the parent to redraw with the new child order.
Arrange them in the order you wants to show. Suppose, you wanna show view 1 on top of view 2. Then write view 2 code then write view 1 code. If you cant does this ordering, then call bringToFront() to the root view of the layout you wants to bring in front.
You can set visibility to false of other views.
view1.setVisibility(View.GONE);
view2.setVisibility(View.GONE);
...
or
view1.setVisibility(View.INVISIBLE);
view2.setVisibility(View.INVISIBLE);
...
and set
viewN.setVisibility(View.VISIBLE);
참고URL : https://stackoverflow.com/questions/6759279/how-to-bring-view-on-front-of-everything
'IT박스' 카테고리의 다른 글
반응-DOM이 렌더링되는 동안 로딩 화면이 표시됩니까? (0) | 2020.07.28 |
---|---|
날짜 문자열을 날짜로 구문 분석하는 방법은 무엇입니까? (0) | 2020.07.27 |
새로운 구문 중 하나 대신 일반 오래된 Thread 객체를 사용하는 것이 더 좋은 경우가 있습니까? (0) | 2020.07.27 |
PHP에서 cURL을 사용한 RAW POST (0) | 2020.07.27 |
Android 용 Eclipse에서 ProGuard 활성화 (0) | 2020.07.27 |