4/4 수업
1. Fragment(Blank) 추가
2. 이름 설정(Red, Blue, Green 3개 만들 예정)
RedFragment.kt
package com.example.fragment_navigation
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class RedFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
// Inflate the layout for this fragment
val root: View = inflater.inflate(R.layout.fragment_red, container, false)
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}
fragment_red.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:id="@+id/greenFrag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4C5EAF"
tools:context=".RedFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="88dp"
android:layout_marginTop="96dp"
android:text="Blue Text"
android:textSize="48sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Blue, Green도 똑같이 해준다.
Navigation 하기 위해 필요한 것
build.gradle(:app)에서
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
이 코드를 넣어주고 Sync 해준다.
-- 만약 test_navi.xml이 로드가 안된다면, 아래에서 invalid caches를 선택해준다.
test_navi.xml에서 fragment 3개를 클릭해서 가져온다.
red를 home으로 설정
화살표 당겨서 red->green->blue 순으로 설정
MainActivity.kt
package com.example.fragment_navigation
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupActionBarWithNavController
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.mainFrag) as NavHostFragment
navController = navHostFragment.navController
setupActionBarWithNavController(navController)
}
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp() || super.onSupportNavigateUp()
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/mainFrag"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/test_navi" />
</androidx.constraintlayout.widget.ConstraintLayout>
'수업' 카테고리의 다른 글
컴퓨터그래픽스 - 12. 그래픽스 기본 이론2 (0) | 2023.04.05 |
---|---|
웹프로그래밍 - 자바스크립트 javascript 기초 (0) | 2023.04.04 |
모바일프로그래밍 - data binding / kotlin 사용, android app 개발 (0) | 2023.04.04 |
android studio / kotlin 기본. 버튼 클릭시 숫자 1증가하는 앱 (0) | 2023.04.03 |
웹프로그래밍 - 4-2. flexbox 아이템에 적용하는 속성들 (0) | 2023.04.03 |