반응형
탭을 구성할때, 프래그먼트를 이용하는것 이 일반적이다.
1) 외부 라이브러리 추가 com.android.support:design
2) bulid.gradle 확인
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "rain2002kr.techworld.myautosmssender_ver1"
minSdkVersion 22
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.android.support:design:28.0.0'
}
3) 보여주기 원하는 Activity에 다음과 같이 셋팅
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:elevation="1dp"
android:id="@+id/toolbar">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.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"
>
</android.support.design.widget.TabLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"
android:id="@+id/container"
/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
4) Activity Java 파일에서 다음과 같이 코딩 Toolbar 설정
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
Toolbar toolbar = (Toolbar) findViewById( R.id.toolbar );
setSupportActionBar(toolbar);
ActionBar abar = getSupportActionBar();
abar.show();
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
manager = getSupportFragmentManager();
manager.beginTransaction().add(R.id.container , fragment1).commit();
TabLayout tabs = (TabLayout) findViewById( R.id.tabs );
tabs.addTab( tabs.newTab().setText( "친구" ) );
tabs.addTab( tabs.newTab().setText( "일대일채팅" ) );
tabs.addTab( tabs.newTab().setText( "기타" ) );
tabs.addTab( tabs.newTab().setText( "기타1" ) );
tabs.setOnTabSelectedListener( new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
Fragment seleted = null;
if (position == 0) {
seleted = fragment1;
} else if (position ==1){
seleted = fragment2;
} else if (position ==2){
seleted = fragment3;
}
manager.beginTransaction().replace( R.id.container, seleted ).commit();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
} );
}
5) 확인
'안드로이드 프로그래밍[JAVA Code] > Menu' 카테고리의 다른 글
Menu : 메뉴 만들기 (0) | 2019.12.03 |
---|