[Kotlin] TabLayout Custom

Android

(Update : 2018-11-02)

Language :

화면

1533383866820_Screenshot_2018-08-04-20-51-42.png

values/ strings.xml

<resources>
    <string name="app_name">TabLayoutCustom</string>

    <string name="one">ONE</string>
    <string name="tow">TOW</string>
    <string name="three">THREE</string>
</resources>

font/ dico_font.xml

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <font
        android:font="@font/dico_normal"
        android:fontStyle="normal"
        android:fontWeight="1"
        app:font="@font/dico_normal"
        app:fontStyle="normal"
        app:fontWeight="1"
        tools:targetApi="o" />
</font-family>

layout/ activity_main.xml

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        app:tabIndicatorColor="@color/dico" />

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val tab: TabLayout = findViewById(R.id.tab)
        tab.addTab(tab.newTab().setText(R.string.one))
        tab.addTab(tab.newTab().setText(R.string.tow))
        tab.addTab(tab.newTab().setText(R.string.three))

        // Tab Custom
        val tabMargin = 64
        val tabStrip: View = tab.getChildAt(0)
        if (tabStrip is ViewGroup) {
            for (i: Int in 0..(tabStrip.childCount - 1)) {
                val tabView: View = tabStrip.getChildAt(i)
                val vg: ViewGroup = tabView as ViewGroup

                // font family
                for (j: Int in 0..(vg.childCount - 1)) {
                    val childView: View = vg.getChildAt(j)
                    if (childView is TextView) {
                        val typeface: Typeface? =
                                ResourcesCompat.getFont(this, R.font.dico_font)
                        childView.setTypeface(typeface, Typeface.NORMAL)
                    }
                }

                // tab Indicator margin
                val tabParam: ViewGroup.LayoutParams = tabView.layoutParams
                if (tabParam is ViewGroup.MarginLayoutParams) {
                    tabParam.leftMargin = tabMargin
                    tabParam.rightMargin = tabMargin
                }
            }
            tab.requestLayout()
        }
    }
}

민갤

Back-End Developer

백엔드 개발자입니다.