我的Android Studio项目中有问题。 我有不同的片段,在那些片段中,我需要在“片段”的工具栏中显示一个菜单,但是找不到找到显示该片段的方法。 我什至看过太多教程并遵循它们的代码,但仍然无法正常工作。
这是“我的活动”代码:
public class HomeActivity extends AppCompatActivity {
public static
Toolbar toolBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
BottomNavigationView bottomnav = findViewById(R.id.HomeNavigation);
bottomnav.setOnNavigationItemSelectedListener(navListener);
Toolbar TB = (Toolbar)findViewById(R.id.toolBar);
setSupportActionBar(TB);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.home_toolbaritems, menu);
return true;
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener
= new BottomNavigationView.OnNavigationItemSelectedListener()
{
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId())
{
case R.id.navhome:
selectedFragment = new HomeFragment();
break;
case R.id.navanalysis:
selectedFragment = new AnalysisFragment();
break;
case R.id.navbinnacle:
selectedFragment = new BinnacleFragment();
break;
case R.id.navsettings:
selectedFragment = new SettingsFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentcontainer
, selectedFragment).commit();
return true;
}
};
这是我的活动的布局代码:
<?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=".HomeActivity">
<RelativeLayout
android:id="@+id/fragmentcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/HomeNavigation"
android:layout_marginBottom="-56dp">
</RelativeLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/HomeNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:menu="@menu/home_bottomnavigation">
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
This is my Fragment's code:
public class HomeFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setHasOptionsMenu(true);
return inflater.inflate(R.layout.homefragment, container, false);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.home_toolbaritems, menu);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.NewItem:
//New Button in toolbar has been clicked
OpenNewDialog();
break;
case R.id.SelectItem:
//Select button in toolbar has been clicked
break;
case R.id.ImportItemHome:
//Select button in toolbar has been clicked
break;
}
return super.onOptionsItemSelected(item);
}
public void OpenNewDialog() {
NewDialogClass newdialog = new NewDialogClass();
newdialog.show(getChildFragmentManager(), "New Dialog");
}
}
这是我的片段的布局代码:
<?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">
<include layout="@layout/mytoolbar_layout" />
<android.support.v7.widget.CardView
android:layout_width="309dp"
android:layout_height="355dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="44dp"
android:layout_marginLeft="44dp"
android:layout_marginTop="200dp"
android:layout_marginEnd="44dp"
android:layout_marginRight="44dp"
android:layout_marginBottom="110dp"
app:cardCornerRadius="40dp"
app:cardElevation="40dp"
app:cardMaxElevation="40dp"
tools:cardBackgroundColor="@color/Greyish" />
</RelativeLayout>