[Android/Kotlin] Round TabLayout ๋งŒ๋“ค๊ธฐ

2020. 12. 30. 21:22ใ†๐Ÿ“ฑAndroid/๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป Android ๊ฐœ๋ฐœ ์ผ์ง€

๋ฐ˜์‘ํ˜•

์ด์ „ ๊ธ€์—์„œ Fragment์™€ ViewPager์„ ์ด์šฉํ•˜์—ฌ TabLayout ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์ด๋ฒˆ ๊ธ€์—์„œ๋Š” ์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์€ Round ๋œ TabLayout ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•ด๋“œ๋ฆฌ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

 

TabLayout ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ž˜ ๋ชจ๋ฅด์‹œ๊ฒ ๋‹ค๋ฉด !? ์•„๋ž˜ ๊ธ€์„ ๋จผ์ € ์ฝ๊ณ  ์˜ค์‹œ๋Š” ๊ฑธ ์ถ”์ฒœ ๋“œ๋ ค์š” ๐Ÿ‘€๐Ÿ‘€

 

[Android/Kotlin] ๊ธฐ๋ณธ TabLayout ๋งŒ๋“ค์–ด๋ณด๊ธฐ

์œ„ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ์ƒ๋‹จ์— Tab๋ฐ”๊ฐ€ ์กด์žฌํ•˜์—ฌ ์Šคํฌ๋กค ์‹œ Tab์— ๋งž๋Š” ํ™”๋ฉด์ด ๋ณด์—ฌ์ง€๋Š” ํ™”๋ฉด ๊ตฌ์„ฑ์„ ๋งŽ์ด ๋ณด์…จ์„ ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ•ด์š”. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ƒ๋‹จ์— TabLayout์ด ์กด์žฌํ•˜๋Š” ๊ธฐ๋ณธ TabLayout์„ ๋งŒ๋“ค์–ด

yunaaaas.tistory.com

 

๊ธฐ๋ณธ TabLayout์„ ๋งŒ๋“œ๋Š” ๋ฐฉ์‹์€ ๋ชจ๋‘ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€์ ์ธ ๋ถ€๋ถ„๋งŒ ์„ค๋ช…ํ•ด๋“œ๋ฆด๊ฒŒ์š”!

 

drawable ํด๋”์— tablayout.xml (tablayout_background) ์ถ”๊ฐ€ ํ•ด์ฃผ๊ธฐ

Round Tab์„ ๋งŒ๋“ค๊ธฐ์œ„ํ•ด TabLayout์— Background๋ฅผ ๋‹ด๋‹นํ•  Drawable์„ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

<solid android:color = ""/> ๋ถ€๋ถ„์— ์›ํ•˜๊ณ ์žํ•˜๋Š” color ์ƒ‰์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ €๋Š” ๊ธฐ๋ณธ color๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <corners android:radius="18dp"/>
    <solid android:color="@color/colorPrimaryDark"/>
</shape>

 

drawable ํด๋”์— selected_tab.xml (์„ ํƒ๋˜์–ด์ง„ tab_background) ์ถ”๊ฐ€ ํ•ด์ฃผ๊ธฐ

์ด๋ฒˆ์—๋Š” ์„ ํƒ๋œ Round Tab์˜ Background๋ฅผ ๋‹ด๋‹นํ•  Drawable์„ ์ถ”๊ฐ€ํ•ด์ฃผ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ €๋Š” ์ƒ‰์˜ ๋Œ€๋น„๋ฅผ ์œ„ํ•ด #ffffff ์ƒ‰์ธ White๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. 

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•˜์‹ค ์ ์€ ๋‘ฅ๊ตด๊ธฐ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” Radius๋ฅผ TabLayout.xml๊ณผ ๋™์ผํ•˜๊ฒŒ ํ•ด์ฃผ์…”์•ผ ๋งค๋„๋Ÿฝ์Šต๋‹ˆ๋‹ค.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <corners android:radius="18dp"/>
    <solid android:color="#ffffff"/>
</shape>

 

drawable ํด๋”์— tab_layout_selector.xml (์•ž์—์„œ ๋งŒ๋“  xml ์—ฐ๊ฒฐํ•ด์ฃผ๊ธฐ) ์ถ”๊ฐ€ ํ•ด์ฃผ๊ธฐ

์„ ํƒ๋œ Tab์€ selected_tab.xml, ๊ทธ๋ ‡์ง€ ์•Š์€ Tab์€ tablayout.xml ๊ณผ ์—ฐ๊ฒฐ ํ•ด์ฃผ๋Š” Drawable์„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/selected_tab"
        android:state_selected="true"/>
    <item
        android:drawable="@drawable/tablayout"
        android:state_selected="false"/>
</selector>

 

activity_main.xml TabLayout ๋ถ€๋ถ„ ์ˆ˜์ •ํ•˜๊ธฐ

์ด์ œ activity_main.xml์—์„œ TabLayout์— ์•ž์—์„œ ๋งŒ๋“ค์—ˆ๋˜ tab_layout_selector.xml๊ณผ ์—ฐ๊ฒฐํ•ด์ฃผ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 <com.google.android.material.tabs.TabLayout
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:id="@+id/Tab"
        android:background="@drawable/tablayout"
        app:tabMode="scrollable"
        app:tabBackground="@drawable/tab_layout_selector"
        app:tabSelectedTextColor="@color/colorPrimaryDark"
        app:tabPaddingStart="16dp"
        app:tabPaddingEnd="16dp"
        app:tabIndicatorHeight="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
  • app:tabBackground : ์„ ํƒ๋œ Tab์˜ Background ์ƒ‰ ์ง€์ •
  • android:background : TabLayout ์ „์ฒด์˜ Background ์ƒ‰ ์ง€์ •
  • app:tabSelectedTextColor : ์„ ํƒ๋œ Tab์˜ Text ์ƒ‰ ์ง€์ •

 

์ฐจ๊ทผ ์ฐจ๊ทผ ๋”ฐ๋ผํ•˜์‹œ๊ฒŒ ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ Round TabLayout์ด ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค : )

 

_ ์ฐธ๊ณ 

TabLayout์—์„œ Dot Indicator์„ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ž˜ ์†Œ๊ฐœ๋œ ํฌ์ŠคํŒ…์ž…๋‹ˆ๋‹ค.

์ฐธ๊ณ ํ•˜์…”์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€์•„๋‹Œ Dot Indicator๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค : )

 

4 steps to Android Dot TabItem

Dot presentation to TabLayout is common in many apps. There are only 4 steps to configure the dots! Click to read more. ๐Ÿ˜

itnext.io

 

๋ฐ˜์‘ํ˜•