728x90
fragment
조각들
조각들을 교체해주는 레이아웃
Framlayout
relative , frame, linear 레이아웃들을 다양하게 섞어서 사용가능
현재 relative 안에 linear가 있어서 linear에서 relative 속성 사용가능
android:layout_alignParentBottom="true"
OncreateView는 Mainactivity.java의 생명주기 ocreate와 같이
fragment의 생명주기임.
Mainactivity
package com.example.fragmentexmaple;
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 btn_1,btn_2,btn_3,btn_4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_1 = (Button)findViewById(R.id.btn_1);
btn_2 = (Button)findViewById(R.id.btn_2);
btn_3 = (Button)findViewById(R.id.btn_3);
btn_4 = (Button)findViewById(R.id.btn_4);
//버튼 클릭했을때 액션 지정
btn_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment1 fragment1 = new Fragment1();
transaction.replace(R.id.frame,fragment1);
transaction.commit();
//바꾼다음에 저장해라 , 새로고침같은 개념
}
});
btn_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment2 fragment2 = new Fragment2();
transaction.replace(R.id.frame,fragment2);
transaction.commit();
//바꾼다음에 저장해라 , 새로고침같은 개념
}
});
btn_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment3 fragment3 = new Fragment3();
transaction.replace(R.id.frame,fragment3);
transaction.commit();
//바꾼다음에 저장해라 , 새로고침같은 개념
}
});
btn_4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment4 fragment4 = new Fragment4();
transaction.replace(R.id.frame,fragment4);
transaction.commit();
//바꾼다음에 저장해라 , 새로고침같은 개념
}
});
}
}
fragment 1,2,3,4 (숫자만바꾸면됨)
package com.example.fragmentexmaple;
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);
}
}
fragment xml 1,2,3,4
<?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:text="나는천재"
android:textSize="30sp"
></TextView>
</FrameLayout>
activity.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>
결과
메뉴1
결과
메뉴2
메뉴3
메뉴4
728x90
'✍2021,2022 > app(android studio)' 카테고리의 다른 글
앱만들기. 14(Thread & Handler ) (0) | 2021.07.17 |
---|---|
앱만들기.13(Log출력,주석다는법) (0) | 2021.07.17 |
1+2주차 개념정리,복습 (0) | 2021.07.14 |
앱만들기 11.(RecyclerView) (0) | 2021.07.13 |
앱만들기10. (카메라) (0) | 2021.07.12 |