본문 바로가기

Android/기억창고

Bottom Naviogation Activity로 App을 생성하고 RecyclerView를 추가하면 위에 여백이 생기고 아래는 BottomNavigationView에 막혀 끝까지 스크롤되지 않는 현상 해결(삽질의 승리^^)

상황

환경: Android Studio 3.5 이상

1. 안드로이드 스튜디오를 실행하고,

2. 기본 Activity를 Bottom Navigation Activity로 선택하고, 프로젝트를 생성

3. HomeFragment를 WordFragment로 변경(layout과 ViewModel로 이름을 맞춰서 Refactoring)

4. fragment_word.xml에 RecyclerView 추가하고 실행하면 그림처럼 됩니다.
   - 꽉채워줬는데도 위가 비고,
   - 아래는 스크롤을 아무리해도 가려지는 부분이 생깁니다.

 

 

 

 

 

 

해결방법

아래의 해결방법이 정답인지는 잘 모르겠지만 일단 해결했습니다.
상단먹어주는 공간은 우연히 해결했습니다. 두번째 아래 스크롤은 stackoverflow를 뒤졌습니다. ^^

1. 상단여백 없애기

다음은 기본생성된 원래 코드입니다.

- Before

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">
    <fragment
    ''''''
    />
    <com.google.android.material.bottomnavigation.BottomNavigationView
        .......
        />
</androidx.constraintlayout.widget.ConstraintLayout >

위의 코드에서 8번째 줄(android:paddingTop="?attr/actionBarSize")을 삭제하면 위쪽 공간은 없어집니다.(아래에 변경된 코드가 있습니다.) 간단하죠?

- After

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
    ''''''
    />
    <com.google.android.material.bottomnavigation.BottomNavigationView
        .......
        />
</androidx.constraintlayout.widget.ConstraintLayout >

2. 스크롤이 BottomNavigationView에 의해 가려지지 않게 하기

이번에는 한줄을 수정하고, 한 줄을 추가해야 합니다. 다음은 원래 코드의 수정대상 부분입니다.

- Before : 1번은 적용된 상태

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />
    <com.google.android.material.bottomnavigation.BottomNavigationView
        ......
        />
</androidx.constraintlayout.widget.ConstraintLayout >

위의 코드에서 1줄을 변경합니다.(android:layout_height="match_parent" -> android:layout_height="0dp"), 그리고 그 밑에 한 줄(app:layout_constraintVertical_weight="1")을 추가합니다.

- After

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintVertical_weight="1"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />
    <com.google.android.material.bottomnavigation.BottomNavigationView
        ......
        />
</androidx.constraintlayout.widget.ConstraintLayout >

저도 안잊어먹을라고 적어봅니다.

마지막으로 결과화면입니다.

혹시라도 도움이 되시기를 바랍니다.

'Android > 기억창고' 카테고리의 다른 글

NavigationDrawer Activity 메뉴 만지작거리기(#1)  (0) 2020.02.22