我想在我的应用中添加一个应用栏,它有一个底部导航栏。我关注了android开发人员的帮助,这是我在活动中所做的:
public class BetsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bets);
BottomNavigationView bottomNavigationView = (BottomNavigationView)
findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_p1:
selectedFragment = p1.newInstance();
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
break;
case R.id.navigation_p2:
selectedFragment = p2.newInstance();
myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment);
transaction.commit();
return true;
}
});
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, p1.newInstance());
transaction.commit();
}
活动布局:
<RelativeLayout
android:id="@+id/activity_main"
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="com.example.carbet.BetsActivity">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/navigation"
android:animateLayoutChanges="true">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
app:itemTextColor="@android:color/white"
app:itemIconTint="@android:color/white"
app:menu="@menu/navigation" />
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</RelativeLayout>
这是我的应用清单:
<application
android:allowBackup="true"
android:icon="@mipmap/logo_car_bet"
android:label="@string/app_name"
android:roundIcon="@mipmap/logo_car_bet"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/logo_car_bet" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<activity
android:name=".MainActivity"
android:label="Car Bet"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BetsActivity"
android:label="@string/title_activity_bets"
android:parentActivityName=".MainActivity"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</application>
我使用片段作为底部导航栏,但我不会在他们的文件中做任何事情。 当我启动它时,首先它显示一个绿色条,但没有任何标题或溢出菜单。当我转到另一个片段时,会出现标题但不会显示菜单。 请告诉我我犯了哪些错误。
答案 0 :(得分:0)
在xml中添加一个带有ToolBar的NumPy
在Java代码中,在AppBarLayout
setContentView
然后给菜单充气。