android studio 4个底部导航栏所有代码
时间: 2023-08-03 09:31:06 浏览: 116
以下是一个简单的Android Studio中使用4个底部导航栏的示例代码:
1. 创建一个新的Android项目并打开MainActivity.java文件。
2. 在MainActivity.java中添加以下代码:
```java
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNav;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_search:
selectedFragment = new SearchFragment();
break;
case R.id.nav_favorites:
selectedFragment = new FavoritesFragment();
break;
case R.id.nav_profile:
selectedFragment = new ProfileFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
}
```
3. 在res/layout文件夹中创建一个新的XML文件并命名为activity_main.xml。将以下代码添加到文件中:
```xml
<androidx.constraintlayout.widget.ConstraintLayout
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">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/white"
app:itemIconTint="@drawable/bottom_nav_color_selector"
app:itemTextColor="@drawable/bottom_nav_color_selector"
app:labelVisibilityMode="labeled"
app:menu="@menu/bottom_nav_menu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
```
4. 在res/menu文件夹中创建一个新的XML文件并命名为bottom_nav_menu.xml。将以下代码添加到文件中:
```xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home"
android:title="@string/home"/>
<item
android:id="@+id/nav_search"
android:icon="@drawable/ic_search"
android:title="@string/search"/>
<item
android:id="@+id/nav_favorites"
android:icon="@drawable/ic_favorites"
android:title="@string/favorites"/>
<item
android:id="@+id/nav_profile"
android:icon="@drawable/ic_profile"
android:title="@string/profile"/>
</menu>
```
5. 在res/drawable文件夹中创建一个新的XML文件并命名为bottom_nav_color_selector.xml。将以下代码添加到文件中:
```xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/primary" android:state_checked="true"/>
<item android:alpha="0.6" android:color="@color/gray" android:state_checked="false"/>
</selector>
```
6. 在res/values/colors.xml文件中定义以下颜色:
```xml
<resources>
<color name="primary">#6200EE</color>
<color name="gray">#757575</color>
<color name="white">#FFFFFF</color>
</resources>
```
7. 在res/values/strings.xml文件中定义以下字符串:
```xml
<resources>
<string name="home">Home</string>
<string name="search">Search</string>
<string name="favorites">Favorites</string>
<string name="profile">Profile</string>
</resources>
```
8. 在res/drawable文件夹中添加以下4个图标:ic_home.png、ic_search.png、ic_favorites.png和ic_profile.png。
9. 运行应用程序并验证底部导航栏是否正常工作。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)