728x90
안녕하세요,
오늘은 CollapsingToolbarLayout 상단바 확장/축소에 따라 상태바 색상 변경을 해보도록 하겠습니다.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private AppBarLayout appBarLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// AppBarLayout 상태 감지
appBarLayout = findViewById(R.id.appbar);
appBarLayout.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
int totalScrollRange = appBarLayout.getTotalScrollRange();
if (Math.abs(verticalOffset) >= totalScrollRange) {
// Toolbar가 완전히 축소되었을 때 → 검정색 상태바
changeStatusBarColor(Color.BLACK);
} else {
// 확장 상태 → 빨간 상태바
changeStatusBarColor(Color.RED);
}
});
}
// 상태바 색상 변경 함수
private void changeStatusBarColor(int color) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(color);
}
}
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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- AppBarLayout -->
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="250dp">
<!-- CollapsingToolbarLayout -->
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<!-- 배경 이미지 -->
<ImageView
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"/>
</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] RecyclerView에서 스와이프로 항목 삭제 (Undo 기능 포함) (0) | 2025.02.10 |
---|---|
[Java][Android] EditText 포커스 해제 시 키보드 숨기기 (2) | 2025.02.06 |
[Java][Android] 현재 앱의 버전 정보 표시하기 (0) | 2025.01.26 |
[Java][Android] 키 해시(Key Hash) 추출 방법 (0) | 2025.01.23 |
[JAVA][Android] CollapsingToolbarLayout을 활용하여 확장/축소되는 상단바 구현하기 (0) | 2025.01.21 |