728x90
반응형
public class MainActivity extends FragmentActivity {
TabLayout tabs;
Fragment1 fragment1;
Fragment2 fragment2;
Fragment3 fragment3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
getSupportFragmentManager().beginTransaction().add(R.id.container, fragment1).commit();
tabs = findViewById(R.id.tabs);
tabs.addTab(tabs.newTab().setText("친구"));
tabs.addTab(tabs.newTab().setText("채팅"));
tabs.addTab(tabs.newTab().setText("더보기"));
tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
Fragment selected = null;
if(position == 0)
selected = fragment1;
else if(position == 1)
selected = fragment2;
else if(position == 2)
selected = fragment3;
getSupportFragmentManager().beginTransaction().replace(R.id.container, selected).commit();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="1dp"
android:background="@android:color/background_light"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabTextColor="@color/colorPrimary"
app:tabSelectedTextColor="@color/colorAccent" />
<FrameLayout
android:id="@+id/container"
android:layout_below="@id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</RelativeLayout>
728x90
반응형
'Android' 카테고리의 다른 글
Android Apk 생성시 이름 바꾸기 (0) | 2021.04.22 |
---|---|
FCM - 디폴트 아이콘 ( MessagingService에서 설정했는데, 백그라운드 일때 아이콘이 바뀌지 않는경우 . ) (0) | 2021.04.21 |
패키지명 변경 (0) | 2021.03.18 |
Android 카메라 / 갤러리 저장하기 ( Q Scoped Storage) (1) | 2021.03.12 |
Sms Retriever ( 안드로이드 문자 메세지 수신 ) (3) | 2021.03.09 |