Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 안드로이드
- 랜덤넘버
- Linux
- Javscript
- Hook
- button
- array
- React
- Java
- 구글맵스
- 리액트
- fragment
- GoogleMaps
- nodejs
- JS
- 오버라이딩
- Kotlin
- TextView
- scrollview
- 랜덤번호
- JavaScript
- TypeScript
- 코틀린
- npm
- RecyclerView
- stylesheet
- 자바스크립트
- SpringBoot
- Android
- 스프링부트
Archives
- Today
- Total
타닥타닥 개발자의 일상
Kotlin Fragment 기능 이용해서 안드로이드 화면에 상단 메뉴 만들고 선택에 따라서 화면전환 하기 본문
코딩 기록/Kotlin
Kotlin Fragment 기능 이용해서 안드로이드 화면에 상단 메뉴 만들고 선택에 따라서 화면전환 하기
NomadHaven 2022. 2. 7. 23:28폴더 및 파일 구성
com.example.sample23 폴더> FragmentMenu , FragmentOne, FragmentTwo, FragmentThee 코틀린 클래스 생성
layout 폴더 > activity_fragment_menu.xml, activity_fragment_one.xml, activity_fragment_two.xml, activity_fragment_three.xml 파일 생성
activity_fragment_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="First"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:onClick="onClick"
android:layout_weight="1"/>
<Button
android:text="Second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:onClick="onClick"
android:layout_weight="1"/>
<Button
android:text="Third"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:onClick="onClick"
android:layout_weight="1"/>
</LinearLayout>
activity_fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="First Fragment"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_fragment_two.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#0000ff" //배경색 설정, 파랑
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Second Fragment"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_fragment_three.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ff0000" // 배경색 설정. 빨강
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Third Fragment"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fragment_menu"
android:name="com.example.sample23.FragmentMenu"
tools:layout="@layout/activity_fragment_menu"
tools:ignore="MissingConstraints"/>
<FrameLayout
android:id="@+id/content"
android:layout_width="598dp"
android:layout_height="910dp"
app:layout_constraintTop_toBottomOf="@+id/fragment_menu"
app:layout_constraintStart_toStartOf="parent">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
FragmentMenu.kt
package com.example.sample23
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class FragmentMenu : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.activity_fragment_menu, container, false)
//Fragment 상속받고 layout 폴더의 activity_fragment_menu.xml 파일과 연결. 이를 화면에 띄운다.
}
}
FragmentOne.kt
package com.example.sample23
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class FragmentOne: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.activity_fragment_one, container, false)
//Fragment 상속받고 layout 폴더의 activity_fragment_one.xml 파일과 연결. 이를 화면에 띄운다.
}
}
FragmentTwo.kt
package com.example.sample23
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class FragmentTwo: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.activity_fragment_two, container, false)
}
}
FragmentThree.kt
package com.example.sample23
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class FragmentThree: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.activity_fragment_three, container, false)
}
}
MainActivity.kt
package com.example.sample23
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val fm = supportFragmentManager
val fragmentTransaction = fm.beginTransaction()
fragmentTransaction.add(R.id.content, FragmentOne())
fragmentTransaction.commit()
}
override fun onClick(v: View?) {
Log.d("버튼 클릭!", "버튼 클릭!")
var fr: Fragment? = null
if(v?.id == R.id.button1){
fr = FragmentOne() //button1이 선택되면 FragmentOne을 보인다
} else if(v?.id == R.id.button2){
fr = FragmentTwo() //button2이 선택되면 FragmentTwo를 보인다
} else if(v?.id == R.id.button3){
fr = FragmentThree() //button3이 선택되면 FragmentThree를 보인다
}
val fm = supportFragmentManager
val fragmentTransaction = fm.beginTransaction()
// fragment 교체
fragmentTransaction.replace(R.id.content, fr!!)
fragmentTransaction.commit()
}
}
결과화면
'코딩 기록 > Kotlin' 카테고리의 다른 글
코틀린 Kotlin seekbar / ratingbar (별점) 안드로이드 화면에 나타내고 textview랑 연결하기 (0) | 2022.02.08 |
---|---|
Kotlin 코틀린 Fragment 이용하여 안드로이드 화면 전환하고 Fragment에 아이콘 삽입하기 (1) | 2022.02.08 |
Kotlin 안드로이드 화면에 WebView이용해서 html 문서 연결하고 구현화면 버튼 클릭시 알림창 띄우기 (0) | 2022.02.07 |
Kotlin 안드로이드 화면에 videoView이용해 동영상 삽입하기 (0) | 2022.02.07 |
Kotlin 정렬된 grid 안드로이드 화면에 표시하기, grid에서 선택한 사항 textview랑 연결하기 (0) | 2022.02.07 |
Comments