728x90
안녕하세요,
많은 앱에서 스크롤을 내리면 상단 부분이 줄어들다가 상단바만 남고 고정되고, 다시 스크롤을 올렸을 때 아래 내용이 다 출력된 후에 상단 부분이 확장되는 기능을 사용하고 있습니다.
오늘은 이와 같은 확장 / 축소되는 상단바가를 만들 수 있는 CollapsingToolbarLayout를 사용해보도록 하겠습니다.
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Toolbar 설정
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// CollapsingToolbar 설정
CollapsingToolbarLayout collapsingToolbar = findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle("테스트");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary"
app:titleEnabled="true">
<!-- 상단 이미지 -->
<ImageView
android:id="@+id/header_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/img"
app:layout_collapseMode="parallax"/>
<!-- Toolbar -->
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<!-- 스크롤 가능한 내용 -->
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="안녕하세요"
android:textSize="24sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="테스트1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="테스트2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="테스트3"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
시연 영상 입니다.
'안드로이드 자바' 카테고리의 다른 글
[Java][Android] 현재 앱의 버전 정보 표시하기 (0) | 2025.01.26 |
---|---|
[Java][Android] 키 해시(Key Hash) 추출 방법 (0) | 2025.01.23 |
[JAVA][Android] AnyChart 로 막대그래프 생성 및 tooltip 활용 (0) | 2025.01.14 |
[Android][JAVA] 접근성 서비스 (Accessibility Service) : 앱에서 이벤트 추적과 자동화 구현하기 (0) | 2025.01.13 |
[Java][Android] ClipboardManager 사용해서 복사 및 붙여넣기 구현하기 (0) | 2025.01.12 |