android-studio - android studio中的底部导航栏
问题描述
我创建了一个简单的应用程序,它有一个底部导航栏。此导航栏有 3 个项目(拨号盘、设备、同步)。我也使用了片段。基本上,当用户单击菜单项(例如:设备)时,应用程序会显示设备片段。请注意,我之前没有实现底部导航栏。但是,当我运行应用程序时,会一直停止。我找不到任何解决方案。谁能帮我这个?
// this is the main activity
package com.example.bottomnavigation;
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;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListner);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new DialPadFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListner =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
case R.id.nav_dial:
selectedFragment = new DialPadFragment();
break;
case R.id.nav_device:
selectedFragment = new DeviceFragment();
break;
case R.id.nav_sync:
selectedFragment = new SyncFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
}
// this is the main activity xml
<?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">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_navigation"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_navigation"
app:itemTextColor="@color/colorBlack"
android:background="?android:attr/windowActionBar"/>
</RelativeLayout>
// This is one of the fragment I have created.
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Device"
android:textSize="30sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
// menu resource file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_dial"
android:icon="@drawable/ic_dialpad_black_24dp"
android:title="@string/dialPad"/>
<item
android:id="@+id/nav_device"
android:icon="@drawable/ic_device_black_24dp"
android:title="@string/device"/>
<item
android:id="@+id/nav_sync"
android:icon="@drawable/ic_sync_black_24dp"
android:title="@string/Sync"/>
</menu>
解决方案
推荐阅读
- python - 如何实现管理员审批系统以在 pythondjango 中发布博客
- python - 使用 boto3 将 gzip 文件上传到 S3 存储桶的最佳方式
- c# - 如何将 MessagePack-CSharp 与字符串 JSON 一起使用?不是 CLI,是 neuecc/MessagePack-CSharp
- sql - 多个结果时的情况
- python-3.x - jupyter notebook 错误权限被拒绝:无标题 IPYNB
- python - 有人可以解释一下我在将这个数据框转换为 Python 中的字典时在做什么
- html - 如何使用 :hover 在 div 中更改我的箭头颜色?
- javascript - 尝试使用类组件中的方法更新状态中的道具
- selenium-webdriver - 在 Azure DevOps 管道上运行的 SpecFlow BDD UI 测试
- spring-boot - 如果在 spring boot hibernate 中违反 saveAll()