티스토리 뷰
build.gradle (Modult: app)
dependencies {
implementation 'com.google.android.material:material:1.1.0'
}
TabLayout, TabItem 는 머티리얼 디자인이므로 의존성 추가
activity_main.xml
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:id="@+id/tabRed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tabGreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green" />
</com.google.android.material.tabs.TabLayout>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
MainActivity.kt
package com.jwsoft.kotlinproject
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.FrameLayout
import com.google.android.material.tabs.TabLayout
class MainActivity : AppCompatActivity() {
private lateinit var tabLayout: TabLayout
private lateinit var frameLayout: FrameLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
frameLayout = findViewById(R.id.frameLayout)
frameLayout.setBackgroundColor(Color.parseColor("#FFBBBB"))
tabLayout = findViewById(R.id.tabLayout)
tabLayout.addTab(tabLayout.newTab().setText("BLUE"))
tabLayout.addTab(tabLayout.newTab().setText("WHITE"))
tabLayout.addTab(tabLayout.newTab().setText("BLACK"))
tabLayout.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener {
override fun onTabReselected(tab: TabLayout.Tab?) {
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
}
override fun onTabSelected(tab: TabLayout.Tab?) {
when (tab!!.position) {
0 -> frameLayout.setBackgroundColor(Color.parseColor("#FFBBBB"))
1 -> frameLayout.setBackgroundColor(Color.parseColor("#BBFFBB"))
2 -> frameLayout.setBackgroundColor(Color.parseColor("#BBBBFF"))
3 -> frameLayout.setBackgroundColor(Color.WHITE)
4 -> frameLayout.setBackgroundColor(Color.BLACK)
}
}
})
}
}
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] CoordinatorLayout + AppBarLayout + Toolbar + RecyclerView (0) | 2020.07.04 |
---|---|
[Kotlin] ViewPager2 + RecyclerView.Adapter (0) | 2020.07.03 |
[Kotlin] FrameLayout (0) | 2020.07.02 |
[Kotlin] Fragment (0) | 2020.07.01 |
[Kotlin] BottomSheetDialogFragment (0) | 2020.07.01 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- java
- coroutine
- James Kim
- MVVM
- Kotlin
- Architecture Pattern
- XML
- CoordinatorLayout
- Android
- ViewPager2
- Intent
- JSONArray
- activity
- 안드로이드 #코틀린 #Android #Kotlin
- View
- Design Pattern
- ArrayList
- JSONObject
- 혀가 길지 않은 개발자
- TabLayout
- DataBinding
- 안드로이드
- Vue.js #Vue.js + javascript
- Livedata
- recyclerview
- fragment
- ViewModel
- 자바
- handler
- 코틀린
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함