English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
먼저 구현해야 할效果图을 보겠습니다:
자료를 참고한 결과, 대부분의 방법이 이렇게 구현되고 있습니다:
浮动의 부분과 동일한 layout을 하나 더 작성하고, 먼저浮动区域的可见성을 gone으로 설정합니다.浮动区域이 상단으로 슬라이딩될 때,浮动区域B의 visible성을 VISIBLE로 설정합니다. 이렇게 되면 상단에 고정된 것처럼 보입니다.
이제 다른 방법을 소개합니다:
design 패키지의 컨트롤을 사용
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.peipei.app.MainActivity" <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="250dp"> <android.support.design.widget.CollapsingToolbarLayout app:layout_scrollFlags="scroll" app:contentScrim="#000000" android:layout_width="match_parent" android:layout_height="220dp"> <TextView android:text="배너 영역" android:gravity="center" android:textColor="#ffffff" android:background="#987545" android:layout_width="match_parent" android:layout_height="220dp"/> </android.support.design.widget.CollapsingToolbarLayout> <TextView android:gravity="center" android:layout_width="match_parent" android:layout_height="30dp" android:text="浮动의 부분"/> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/text_margin" android:text="@string/large_text"/> </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout>
구현 단계:
1、悬浮이 필요한 레이아웃을 CollapsingToolbarLayout 밖, AppBarLayout 안에 배치
2、CollapsingToolbarLayout의 app:layout_scrollFlags를 scroll로 설정
3、롤링 NestedScroolView에 설정
app:layout_behavior="@String/appbar_scrolling_view_behavior"
대성공했습니다(CoordinatorLayout의 뿌리 레이아웃을 기억하세요)
최종 결과:
결론
이제 Android 슬라이딩으로 상단 고정 효과에 대한 모든 내용이 끝났습니다. 모두가 배웠나요? 이 기사가 여러분의 학습이나 업무에 도움이 되길 바랍니다. 의문이 있으면 댓글을 통해 교류해 주세요.