ํญ ๋ฒํผ(TAB)
ํญ์ sdk๊ฐ ์ ๊ณตํ๋ ์์ ฏ์ ์ฌ์ฉํ ์๋ ์๊ณ , ์ง์ ๋ง๋ค ์๋ ์๋ค.
ํญ์ ์นด์นด์คํก, ์ธ์คํ๊ทธ๋จ, ํ์ด์ค๋ถ๊ณผ ๊ฐ์ sns์ ๋ง์ ์ดํ์ ํ๋ฉด ๊ตฌ์ฑ์ ํ๋ฐํ๊ฒ ์ฌ์ฉ๋๊ณ ์๋ค.
ํญ์ ๊ตฌ์ฑ๋ฐฉ๋ฒ์ผ๋ก๋ activity๋ฒํผ๊ณผ famelayout(ํ๋ ์ ๋ ์ด์์)์ด๋ผ๋ ์์ ฏ์ ๋ฐฐ์นํ์ฌ ์ ์ฒด์ ์ธ ๋ ์ด์์์ ๊ตฌ์ฑํ์ฌ ๋ฒํผ ์ ํ์, ํด๋น๋๋ fragment๋ฅผ ๋์ฐ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํ๋๋ค.
MainActivity.java
package com.example.actionbar_ex;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.ScrollingTabContainerView;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
Fragment1 fragment1;
Fragment2 fragment2;
Fragment3 fragment3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
getSupportFragmentManager().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.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) {
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:id="@+id/toolbar">
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabTextColor="@color/colorPrimary"
app:tabSelectedTextColor="@color/colorAccent">
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/container">
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</RelativeLayout>
fragment.xml (1,2,3 ํ์ ๋์ผ)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="593dp"
android:gravity="center"
android:text="@string/fragment1"
android:textColor="#000000"
android:textSize="30sp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/next">
</Button>
</LinearLayout>
Fragment.java(1,2,3 ๋์ผ)
package com.example.actionbar_ex;
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) {
ViewGroup rootview = (ViewGroup) inflater.inflate(R.layout.fragment1, container, false);
return rootview;
}
}
!๋ด๊ฐ ๋ฐ๊ฒฌํ ์ค๋ฅ!
xmlํ์ผ์์ android:text="next"์์ ์ค๋ฅ๊ฐ ๋ฐ์ํด์
strings.xml์ด๋ผ๋ ํ์ผ์ ๋ฐ๋ก ์์ฑํ์ฌ ์คํธ๋ง์ผ๋ก ์ธ ์ ์๊ฒ ๋ง๋ค์๋ค.
fragment.xmlํ์ผ์์ android:text="@string/next"๋ก ์์ฑํด์ฃผ๋ฉด ์ค๋ฅ๊ฐ ํด๊ฒฐ๋๋ค.
<resources>
<string name="app_name">Actionbar_ex</string>
<string name="fragment2">fragment2</string>
<string name="next">next</string>
<string name="fragment3">fragment3</string>
<string name="fragment1">fragment1</string>
</resources>
'APP > ANDROID' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ถ์คํธ์ฝ์ค] ๋คํธ์ํน/HTTP (0) | 2020.03.05 |
---|---|
[๋ถ์คํธ์ฝ์ค] ์ค๋ ๋(Thread) (0) | 2020.03.04 |
[๋ถ์คํธ์ฝ์ค]์๋๋ก์ด๋ ํ๋๊ทธ๋จผํธ(fragment),์ก์ ๋ฐ (0) | 2020.02.27 |
[๋ถ์คํธ์ฝ์ค] ๋ธ๋ก๋์บ์คํธ ์์ ์/์ํ ๊ถํ (0) | 2020.02.21 |
[๋ถ์คํธ์ฝ์ค] ์ธํ ํธ(intent)๋ฅผ ์ด์ฉํ ํ๋ฉด์ ํ (0) | 2020.02.18 |