๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Android] TabLayout, TabItem ์›ํ•˜๋Š” UI ๋งŒ๋“ค๊ธฐ

์‚ฝ์งˆํ•˜๋ฉด์„œ ๊ฒฐ๊ตญ์—” ์›ํ•˜๋Š” UI๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ์‚ฝ์งˆํ•˜๋ฉฐ ์•Œ๊ฒŒ๋œ ๊ฒƒ๋“ค์„ ๊ธฐ๋กํ•œ๋‹ค.

 

์›ํ•˜๋˜ ๋ชจ์Šต

1. ํƒญ๋“ค ๋ฐ”๋‹ฅ์—๋Š” ๋ผ์ธ์ด ๊น”๋ ค์žˆ๋‹ค.

2. ์ธ๋””์ผ€์ดํ„ฐ ๊ธธ์ด๊ฐ€ ํƒญ ํ•˜๋‚˜์˜ ๋„“์ด๋ณด๋‹ค๋Š” ์ข์ง€๋งŒ ํ…์ŠคํŠธ ๋„ˆ๋น„๋ณด๋‹ค๋Š” ํฌ๋‹ค

3. ํƒญ ๊ฐ„ ๋””๋ฐ”์ด๋”๊ฐ€ ์žˆ๋‹ค.

 

๊ฒฐ๊ณผ ์ฝ”๋“œ

layout xml ์ฝ”๋“œ

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/target"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@drawable/bg_tablayout"
        app:tabIndicator="@drawable/indicator_2">

        <com.google.android.material.tabs.TabItem
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="๋”ธ๊ธฐ" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="์‚ฌ๊ณผ" />

    </com.google.android.material.tabs.TabLayout>

๋””๋ฐ”์ด๋” ์‚ฝ์ž… ์ฝ”๋“œ

์€์ธ : https://ovso.github.io/blog/2018/11/06/tablayout_divider/

 

TabLayout์— ๊ตฌ๋ถ„์„ (Divider) ๋„ฃ๊ธฐ

TabLayout์— ๊ตฌ๋ถ„์„  ๋„ฃ๊ธฐ

ovso.github.io

 

์‚ฌ์šฉ๋œ drawable ํŒŒ์ผ๋“ค

1) ํฐ ๋ฐฐ๊ฒฝ์— ๋ฐ”๋‹ฅ ์ค„ 

ํฐ ๋ฐฐ๊ฒฝ์— ๋ฐ”๋‹ฅ ์ค„ ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ ์‹ค์ œ๋กœ ๋ฐ”๋‹ฅ์— ํฐ์ƒ‰์„ ๋ฐ”๋‹ฅ์—์„œ 3dp ๋„์›Œ์˜ฌ๋ฆผ

ํฐ ๋ฐฐ๊ฒฝ์— ๋ฐ”๋‹ฅ ์ค„ ๊ทธ๋ฆฌ๋Š” ๋ฐฉ์‹์œผ๋กœ, ํฐ ๋ฐฐ๊ฒฝ์— ํ…Œ๋‘๋ฆฌ ์ƒ‰์„ ๋„ฃ์€ ์‚ฌ๊ฐํ˜• shape์˜ ์œ„์™€ ์˜† ๋งˆ์ง„ ๊ฐ’์„ ๋งˆ์ด๋„ˆ์Šค ๊ฐ’์„ ์ฃผ๋Š” ๋ฐฉ์‹๋„ ์žˆ๋‹ค. ์ •๋ง ์ด๋ ‡๊ฒŒ ๋ฐ–์— ๊ทธ๋ฆฌ์ง€ ๋ชปํ•˜๋Š”๊ฐ€... ๊ตฌ๊ธ€๋งํ•ด๋„ ๋”ฑํžˆ ๋” ์ข‹์€ ๋ฐฉ๋ฒ•๋“ค์„ ๋ชป ์ฐพ๊ฒ ๋‹ค.

 

2) ์ธ๋””์ผ€์ดํ„ฐ

์ธ๋””์ผ€์ดํ„ฐ drawable

์€์ธ : https://sweetcoding.tistory.com/162

 

Android tab bottom indicator change shape (์•ˆ๋“œ๋กœ์ด๋“œ Tab Indicator ๋ชจ์–‘ ๋ณ€๊ฒฝ ํ•˜๊ธฐ)

์šฐ๋ฆฌ๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ ํ•˜๋Š” Tab Layout ์„ ์ ์šฉ ํ•˜๋ฉด ์ด๋Ÿฐ ๋ชจ์Šต๋‹ˆ๋‹ค. ํ•˜๋‹จ์˜ Indicator ๋ชจ์–‘์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€๊ฒฝ ํ•˜๊ณ  ์‹ถ์œผ๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ? ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. 1. tab_selector.xml ์„ drawable ํด๋”์—

sweetcoding.tistory.com

์€์ธ์ด ์จ์ฃผ์‹  ํ˜•์‹(๊ณ„์ธต๊ตฌ์กฐ)๋Œ€๋กœ ์ธ๋””์ผ€์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ์ง€ ์•Š์•„์„œ ์‚ฝ์งˆํ•ด์˜จ ๊ฑฐ์˜€๋‹ค.

 

3) ๋””๋ฐ”์ด๋”

๋””๋ฐ”์ด๋”๋ฅผ ๊ทธ๋ฆฌ๋Š”๋ฐ ๊ตฌ์ง€ custom drawable์„ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ๋‹ค. ์œ„์˜ ๋””๋ฐ”์ด๋” ์‚ฝ์ž… ์ฝ”๋“œ ์ด๋ฏธ์ง€์—์„œ ์ฃผ์„์ฒ˜๋ฆฌ๋œ ๋ถ€๋ถ„์œผ๋กœ ๋””๋ฐ”์ด๋”์˜ width ๋ถ€๋ถ„ ์‚ฌ์ด์ฆˆ ์กฐ์ • ๊ฐ€๋Šฅํ•˜๋‹ค. ๋‚˜๋Š” drawable์—์„œ width ์‚ฌ์ด์ฆˆ๋ฅผ ์žก์œผ๋‹ˆ ์ฝ”๋“œ ์ƒ ๋”ฐ๋กœ ๋””๋ฐ”์ด๋”์˜ width๋ฅผ ์กฐ์ •ํ•  ํ•„์š”๊ฐ€ ์—†์—ˆ๋‹ค.

๋””๋ฐ”์ด๋” width ์‚ฌ์ด์ฆˆ ์ง€์ •

 

์‚ฝ์งˆ๋กœ ์•Œ๊ฒŒ๋œ ๊ฒƒ๋“ค

TabLayout ์†์„ฑ app:tabIndicatorFullWidth="false"

tabIndicatorFullWidth ์†์„ฑ์œผ๋กœ ์ธ๋””์ผ€์ดํ„ฐ์˜ ๋„ˆ๋น„๋ฅผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. 

true > ํƒญ ๋„ˆ๋น„ ๊ฝ‰ ์ฑ„์›€

false > ํƒญ์˜ text ๋„ˆ๋น„ ๋งŒํผ ์ฑ„์›€

app :tabIndicatorFullWidth ="true" ํ˜น์€ ๋ฏธ์„ค์ •
app :tabIndicatorFullWidth ="false"
app :tabIndicatorFullWidth ="false" + android:text="    ๋”ธ๊ธฐ   "

 

TabItem ์†์„ฑ android:layout + TabLayout ์†์„ฑ app:tabIndicatorHeight

TabItem ์˜ ์†์„ฑ์— layout xml ํŒŒ์ผ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๋„ฃ์–ด TabItem์˜ ๋ ˆ์ด์•„์›ƒ์„ ์ปค์Šคํ…€ํ•  ์ˆ˜ ์žˆ๋‹ค.

TabLayout์˜ ์†์„ฑ์œผ๋กœ ์ธ๋””์ผ€์ดํ„ฐ์˜ ๋†’์ด๋ฅผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. 

๋‚˜๋Š” ์ด ์˜ˆ์ œ์—์„œ ์ธ๋””์ผ€์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹ ์—, ์ปค์Šคํ…€ ๋ ˆ์ด์•„์›ƒ์˜ ํ•˜๋‹จ์— ๊ณ ์ • ๋ทฐ๋ฅผ ๋งŒ๋“ค๊ณ , select ์ƒํƒœ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์ปค์Šคํ…€ ๋ ˆ์ด์•„์›ƒ์˜ ํ•˜๋‹จ ๋ทฐ๊ฐ€ ๋…ธ์ถœ๋˜๊ฑฐ๋‚˜ ๋…ธ์ถœ๋˜์ง€ ์•Š๋„๋ก ํ–ˆ๋‹ค.

TabItem์—์„œ android:layout="@layout/tabitem" + TabLayout์—์„œ app:tabIndicatorHeight="0dp"

 

app:tabRippleColor์†์„ฑ์— ํˆฌ๋ช…ํ•œ ๊ฐ’์„ ๋„ฃ์–ด ํƒญํ•  ๋•Œ์˜ ๋ฆฌํ”Œํšจ๊ณผ๋ฅผ ์—†์•ด๋‹ค.
TabItem์˜ ์ปค์Šคํ…€ ๋ ˆ์ด์•„์›ƒ

TabItem์˜ ์ปค์Šคํ…€ ๋ ˆ์ด์•„์›ƒ์—์„œ TextView์˜ ์•„์ด๋””๋ฅผ "@android:id/text1"๋ผ๊ณ  ์จ์•ผ์ง€ TabItem์˜ android:text ์†์„ฑ์— ๋„ฃ์€ ๊ฐ’์ด ์ •์ƒ์ ์œผ๋กœ ์ปค์Šคํ…€ ๋ ˆ์ด์•„์›ƒ์—์„œ ๋‚˜์˜จ๋‹ค. ์ค‘์š”.

์€์ธ : https://stackoverflow.com/a/38035415/9287140

 

How is TabItem used when placed in the layout XML?

The TabLayout documentation gives an example of nesting TabItem directly inside TabLayout like so:

stackoverflow.com

ํ•˜๋‹จ ๋ทฐ์˜ background๋Š” selector drawable๋กœ ๋ถ€๋ชจ์˜ ์†์„ฑ์„ android:duplicateParentState="true"๋กœ ๋ฐ›์•„์„œ ๋™์ž‘ํ•˜๊ฒŒ ํ–ˆ๋‹ค.

๋ทฐ select ์ƒํƒœ ๋”ฐ๋ผ drawable ๊ตฌ๋ถ„