728x90
반응형
**fragment1.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00BCD4"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="첫번째"
android:textSize="50sp" />
</LinearLayout>
소스
**MainActivity.java**
package com.threedpit.mytab;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
Fragment1 fragment1;
Fragment2 fragment2;
Fragment3 fragment3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar =findViewById(R.id.toolbar);
setSupportActionBar(toolbar);// 툴바가 화면에 보임
//getSupportActionBar() 우리가 액션바 설정한것이 여기로 참조해서 가리킴
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);// 타이틀을 보여주게 할꺼냐 이런거
fragment1=new Fragment1();
fragment2=new Fragment2();
fragment3=new Fragment3();
// getSupportFragmentManager().프래그먼트 매니저 찾고
//이렇게 하면 첫번째 프레그먼트가 프레임레이아웃 안에 나옴
getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment1).commit();
TabLayout tabs = findViewById(R.id.tabs);//탭버튼을 위한 위젯을 받아오는것
tabs.addTab(tabs.newTab().setText("통화기록"));//이것은 탭버튼을 추가하는데 통화기록글자 보이게 해라
//아이콘이나 글자도 넣을수 있다.
tabs.addTab(tabs.newTab().setText("스팸기록"));
tabs.addTab(tabs.newTab().setText("연락처"));
tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
// 이함수는 탭버튼이 선택되었을때 동작하는것
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
// 몇번째 탭인지 안드로이드에서는 인덱스를 포지션으로 쓴다.
if(position==0){
//프래그먼트1 번 화면으로 전환
getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment1).commit();
}
else if(position==1){
//프래그먼트2 번 화면으로 전화
getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment2).commit();
}
else if(position==2){
//프래그먼트3 번 화면으로 전화
getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment3).commit();
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
**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"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
//AppBarLayout은 액션바의 영역을 가리킨다고 생각하면됨
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
//Toolbar는 아이콘이 들어가는 영역
//elevation은 앞으로 올라온듯한 느낌을 주는것
//툴바쪽 타이틀 이름 생성되는 부분
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:elevation="1dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<TextView
android:id="@+id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="타이틀"
android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"/>
</androidx.appcompat.widget.Toolbar>
//탭에 대한 정보
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:elevation="1dp"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorAccent"
app:tabTextColor="@color/colorPrimary"/>
</com.google.android.material.appbar.AppBarLayout>
//여기 기준으로 위에는 상단 액션바 부분
// 여기 기준으로 아래는 하단의 화면 전체
// 실제 화면 영역
//CoordinatorLayout 안쪽에 화면 구성요소를 넣은 다음에
//거기에 app:layout_behavior="@string/appbar_scrolling_view_behavior">
// 이것을 넣게 되면 자동으로 상단에 앱바 레이아웃이 차지하고 잇는 나머지 영역을
//계산해서 차지하게 된다.
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</RelativeLayout>
**Fragment1**
package com.threedpit.mytab;
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 {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1,container,false);
}
}
**fragment1.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00BCD4"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="첫번째"
android:textSize="50sp" />
</LinearLayout>
728x90
반응형
'안드로이드(Android)' 카테고리의 다른 글
안드로이드 이론 빡공 15 (0) | 2020.06.23 |
---|---|
안드로이드 이론 빡공 14 (0) | 2020.06.22 |
안드로이드 이론 빡공 12 (0) | 2020.06.19 |
안드로이드 이론 빡공 11 (0) | 2020.06.17 |
안드로이드 이론 빡공 10 (0) | 2020.06.16 |
댓글