컴공과컴맹효묘의블로그

Android- DrawerLayout 본문

컴퓨터/Android Studio

Android- DrawerLayout

효묘 2022. 3. 9. 13:58
반응형

DrawerLayout

드로워 레이아웃은 보통 네비게이션 레이아웃을 설정할 때 쓰인다.
androidx.drawerlayout.widget.DrawerLayout

드로워 레이아웃을 최상위에 선언한다. 그리고 drawer로 쓸 레이아웃에 android:layout_gravity="start"를 사용하면 된다.

이 속성은 드로워가 어디서 나올지 결정한다. 국가의 언어의 방향마다 기본 설정이 바뀐다. 한국은 좌측이다.

<!--activity_main.xml-->
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:fitsSystemWindows="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:fitsSystemWindows="true"
            style="@style/Widget.MaterialComponents.Toolbar.Primary"/>
        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/fragmentView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
    <include
        android:id="@+id/drawerInclude"
        layout="@layout/drawer_main"/>
</androidx.drawerlayout.widget.DrawerLayout>
<!--drawer_main-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:orientation="vertical"
    android:background="?android:attr/colorBackground"
    android:fitsSystemWindows="true">
    <ImageView
        android:layout_marginTop="40dp"
        android:layout_marginStart="80dp"
        android:layout_marginEnd="80dp"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/ic_baseline_account_circle_24"
        android:contentDescription="userProfileImage" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="user님으로 로그인 되었습니다."
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_gravity="center"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/cardview_shadow_start_color"/>

</LinearLayout>
class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var toggle: ActionBarDrawerToggle
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setSupportActionBar(binding.toolbar)

        toggle = ActionBarDrawerToggle(this, binding.drawer, R.string.drawer_opened, R.string.drawer_closed) // toolbar와 drawer 연결

        supportActionBar?.setDisplayHomeAsUpEnabled(true) // 뒤로 가기 버튼 만들기
        toggle.syncState() // 메뉴 모양

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if(toggle.onOptionsItemSelected(item)) {
            return true
        }
        return super.onOptionsItemSelected(item)
    }
}
반응형
Comments