์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- Streamlit๊ธฐ์ด
- ํ์ด์ฌ
- ์๋๋ก์ด๋ ์คํ๋์ค
- ์๋ฐ์คํฌ๋ฆฝํธ ๊ณต๋ถ
- ์๋ฐ์ด๋ณด
- ์น๋์๋ณด๋ ์ ์
- Matplotlib ๊ธฐ์ด
- serverless
- MySQL
- java
- ์๋ฐ
- Android
- RESTful API
- db
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ํ๋ค์ค
- ์๋ฐ๊ธฐ์ด
- ์๋ฐํ๋ก๊ทธ๋๋ฐ
- Streamlit๊ธฐ๋ณธ
- ์๋ฐ๊ณต๋ถ
- ์๋ฐ์คํฌ๋ฆฝํธ
- JavaScript
- ์คํธ๋ฆผ๋ฆฟ
- ํ๋ค์ค๊ณต๋ถ
- ์คํธ๋ฆผ๋ฆฟ ๊ธฐ๋ณธ
- Pandas
- ์๋๋ก์ด๋ ์ฑ ๊ฐ๋ฐ
- ์น๋์๋ณด๋ ๊ธฐ์ด
- streamlit
- ํ์ด์ฌ๊ณต๋ถ
- Today
- Total
ruriruriya
[Android] ์๋๋ก์ด๋ - ํ๋จ ํญ๋ฐ, ๋ค๋น๊ฒ์ด์ ๋ฐ ๋ง๋๋ ๋ฐฉ๋ฒ BottomNavigationView ๋ณธ๋ฌธ
[Android] ์๋๋ก์ด๋ - ํ๋จ ํญ๋ฐ, ๋ค๋น๊ฒ์ด์ ๋ฐ ๋ง๋๋ ๋ฐฉ๋ฒ BottomNavigationView
๋ฃจ๋ฆฌ์ผใ 2024. 1. 9. 18:01
์๋๋ก์ด๋ ์ฑ ๊ฐ๋ฐ ์ ํ๋จ ํญ๋ฐ๋ฅผ ์ด๋ ต์ง ์๊ฒ ๋ง๋ค ์ ์๋ค.
ํญ๋ฐ๋ ์ฃผ๋ก ๋ค๋น๊ฒ์ด์
์ ๊ธฐ๋ฅ์ํ์ฌ ์ฌ์ฉ์์ ํธ์๋ฅผ ๋๋๋ค.
1. build.gradle.kts ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น
์ต์ ๋ฒ์ ์ผ๋ก ์
๊ทธ๋ ์ด๋ ๋์ด ์์ ์ ์์ผ๋
๋ฒ์ ๋ฒํธ๋ฅผ ํ์ธํ์ฌ ๋ณ๊ฒฝํ ์ ์๋๋ก ํ๋ค.
dependencies {
implementation("androidx.navigation:navigation-fragment:2.7.6")
implementation("androidx.navigation:navigation-ui:2.7.6")
}
2. activity_main.xml
2.1. ๋ ์ด์์ ๋ณ๊ฒฝ
ConstraintLayout์ RelativeLayout์ผ๋ก ๋ณ๊ฒฝํด์ค๋ค.
<?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">
</RelativeLayout>
2.2. BottomNavigationView ์์ฑ
๋ค๋น๊ฒ์ด์
์ด ๋ณด์ผ BottomNavigationView์ ์์ฑํ์ฌ
๋ ์ด์์ ์คํ์ผ ๊ฐ์ ์๋์ ๊ฐ์ด ์ง์ ํด์ค๋ค.
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FF5252"
<?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">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FF5252" />
</RelativeLayout>
3.Navigation Resource File ์์ฑ
Resource Manager-Navigation- + ํ๋ฌ์ค ๋ฒํผ ํด๋ฆญ
๋ฆฌ์์ค ํ์ผ ์ด๋ฆ ์ค์ -OK
์์ฑ๋ ํ์ผ ํด๋ฆญ
Create new destination
Fragment (Blank) ์ ํ
Fargment Name ์ค์
์ด๋ ๊ฒ ์ด ๋ค๋น๊ฒ์ด์
์์ด์ฝ ์ ๋งํผ ๋ง๋ ๋ค.
๋ณธ์ธ์ ์ด 3๊ฐ๋ฅผ ๋ง๋ค์๋ค.
4. activity_main.xml Fragment ๋ฐฐ์น
activity_main.xml์์ NavHostFragment๋ฅผ
RelativeLayout์ ๋์ด๋ค ๋๋๋ค.
Navigation Graphs์์ ์๊น ์์ฑํ my_nav.xml๋ฅผ ์ ํํ๋ค.
activity_main.xml์์ ์ด Fragment์ id์ ์์น(layout_avove)๋ฅผ ์ค์ ํด์ค๋ค.
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottomNavigationView"
app:navGraph="@navigation/my_nav"/>
5. Menu ๋ง๋ค๊ธฐ
Resource Manager์์ ๋๋ณด๊ธฐ ๋๋ฌ Menu ์ ํ ํ
+ ๋ฒํผ ๋๋ฌ์ Menu Resource File์ ์๋ก ์์ฑํ๋ค.
ํ์ผ๋ช ์ค์ ํ OK
์ค๋ฅธ์ชฝ ์๋จ ๋์์ธ ๋ณด๊ธฐ๋ก ๋ฐ๊พธ๊ณ
ํญ์ด 3๊ฐ์ด๋ฏ๋ก ๋ฉ๋ด์์ดํ
3๊ฐ ์ถ๊ฐํ๋ค.
item๋ค์ Common Attributes๋ฅผ id, title, icon์ ์ค์ ํด์ค๋ค.
์์ด์ฝ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ๋ค.
https://ruriruriya.tistory.com/147
6. activity_main.xml์์ BottomNavigationView ๋ฉ๋ด ์ธํ
label์ ๋ณด์ฌ์ฃผ๊ธฐ ์ฌ๋ถ๋ฅผ ์ค์
app:labelVisibilityMode="unlabeled"
menu ์ ์๊น ๋ง๋ Menu Resource ์ง์ .
app:menu="@menu/bottom_menu"
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FF5252"
app:labelVisibilityMode="unlabeled"
app:menu="@menu/bottom_menu" />
7. MainActivity.java ์ธํ
์๋์ ๊ฐ์ด ์ฝ๋๋ก ์ธํ ํ๋ค.
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.MenuItem;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
public class MainActivity extends AppCompatActivity {
// ์๋ ์์ด์ฝ ๋์ค๋ ์์ญ
BottomNavigationView bottomNavigationView;
// ๊ฐ ํ๋ ๊ทธ๋จผํธ๋ฅผ ๋ฉค๋ฒ๋ณ์๋ก ๋ง๋ ๋ค.
Fragment firstFragment;
Fragment secondFragment;
Fragment thirdFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.bottomNavigationView);
firstFragment = new FirstFragment();
secondFragment = new SecondFragment();
thirdFragment = new ThirdFragment();
bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
Fragment fragment = null;
if(itemId == R.id.firstFragment){
fragment = firstFragment;
} else if (itemId == R.id.secondFragment) {
fragment = secondFragment;
} else if (itemId == R.id.thirdFragment) {
fragment = thirdFragment;
}
return loadFragment(fragment);
}
});
}
boolean loadFragment(Fragment fragment){
if(fragment != null){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment,fragment)
.commit();
return true;
}else {
return false;
}
}
}
8. ํ๋ฉด ํ์ธ