728x90
기존 액티비티에서 화면 다 교체했지만
이건 한 액티비티에서 화면만 바꿈 (조각 바꿔 입음)
activity.xml
RelativeLayout으로 변경
Fragment를 쓸 때는 항상 FrameLayout으로 조각들을 교체해 주는 레이아웃 필요.
LinearLayout과 섞어 쓸 수 있다.
android:layout_alignParentBottom="true"
밑으로 내려가게 된다. RelativeLayout 속성
fragment1.xml
교체될 조각 Layout 만들어주기
fragement1 만든 후 Ctrl+ c + v 해서 fragment2 만들기
MainActivity.java
버튼 초기화
Fragment1에서의 onCreateView는 MainActivity에서의 onCreateView와 거의 동일하다.
Activity에서 실행될 때, Fragment에서 실행될 때만 다름.
FrameLayout으로 씌워져있는 화면 전환 View change
ex)메뉴바 조각조각으로 내서 인스타그램 같은 곳에서도 많이
코드
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
>
<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="메뉴1"/>
<Button
android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="메뉴2"/>
<Button
android:id="@+id/btn_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="메뉴3"/>
<Button
android:id="@+id/btn_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="메뉴4"/>
</LinearLayout>
</RelativeLayout>
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="hessepark 레게노"/>
</FrameLayout>
Fragment1.java
package com.example.fragmentexample;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Fragment1 extends Fragment {
public Fragment1() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}
MainActivity.java
package com.example.fragmentexample;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn1, btn2, btn3, btn4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn_1);
btn2 = (Button) findViewById(R.id.btn_2);
btn3 = (Button) findViewById(R.id.btn_3);
btn4 = (Button) findViewById(R.id.btn_4);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment1 fragment1 = new Fragment1();
transaction.replace(R.id.frame, fragment1);//onClick눌렀을 때 create쪽 OnClickView타게 해라
//transaction.addToBackStack(null);//뒤 쪽 스택으로 이동해라
transaction.commit();//새로고침 같은 개념. 저장을 해라
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment2 fragment2 = new Fragment2();
transaction.replace(R.id.frame, fragment2);//onClick눌렀을 때 create쪽 OnClickView타게 해라
// transaction.addToBackStack(null);//뒤 쪽 스택으로 이동해라
transaction.commit();//새로고침 같은 개념. 저장을 해라
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment3 fragment3 = new Fragment3();
transaction.replace(R.id.frame, fragment3);//onClick눌렀을 때 create쪽 OnClickView타게 해라
//transaction.addToBackStack(null);//뒤 쪽 스택으로 이동해라
transaction.commit();//새로고침 같은 개념. 저장을 해라
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment4 fragment4 = new Fragment4();
transaction.replace(R.id.frame, fragment4);//onClick눌렀을 때 create쪽 OnClickView타게 해라
//transaction.addToBackStack(null);//뒤 쪽 스택으로 이동해라
transaction.commit();//새로고침 같은 개념. 저장을 해라
}
});
}
}728x90
'[Android Studio] (Java)' 카테고리의 다른 글
| [Android Stuido] (Thread&Handler 사용법) (0) | 2023.08.28 |
|---|---|
| [Android Studio] (Log출력 및 주석 다는 법) (0) | 2023.08.27 |
| [Android Studio] (RecyclerView 수정 필요) (0) | 2023.08.27 |
| [Android Studio] (Navigation Menu 커스텀) (0) | 2023.08.26 |
| [Android Studio] (WebView) (0) | 2023.08.26 |