BottomNavigationView添加片段后不会出现在底部

时间:2017-04-06 14:31:26

标签: android android-fragments

我是android的初学者,今天我创建了一个BottomNavigationView活动,我想用导航按钮显示3个不同的标签,所以我创建了3个片段,问题是在添加片段后,BottomNavigationView显示在顶部如果我想底部的BottomNavigationView就像添加碎片之前那样,我该怎么办?like this  这是我的主要活动代码

package com.hackerinside.jaisonjoseph.polysocial;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private TextView mTextMessage;

public BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:

                tab1 radio = new tab1();
                android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
                manager.beginTransaction().replace(R.id.container, radio, radio.getTag()).commit();


            case R.id.navigation_dashboard:


                tab2 radio1 = new tab2();
                android.support.v4.app.FragmentManager manager1 = getSupportFragmentManager();
                manager1.beginTransaction().replace(R.id.container, radio1, radio1.getTag()).commit();

            case R.id.navigation_notifications:

                tab3 radio2 = new tab3();
                android.support.v4.app.FragmentManager manager2 = getSupportFragmentManager();
                manager2.beginTransaction().replace(R.id.container, radio2, radio2.getTag()).commit();



        }
        return false;
    }

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);






    tab1 radio = new tab1();
    android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.container, radio, radio.getTag()).commit();

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

 }

这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hackerinside.jaisonjoseph.polysocial.MainActivity">

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@android:color/holo_blue_dark">

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="@string/title_home" />

</FrameLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="?android:attr/windowBackground"
    app:menu="@menu/navigation" />

这是我的第一个片段tab1

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.hackerinside.jaisonjoseph.polysocial.tab1">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

2 个答案:

答案 0 :(得分:3)

BottomNavigationView不会自动显示在视图的底部。你必须手动放置它们。 您可以使用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:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.hackerinside.jaisonjoseph.polysocial.MainActivity">

  <FrameLayout
      android:id="@+id/content"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:background="@android:color/holo_blue_dark">

      <TextView
          android:id="@+id/message"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="@dimen/activity_vertical_margin"
          android:layout_marginLeft="@dimen/activity_horizontal_margin"
          android:layout_marginRight="@dimen/activity_horizontal_margin"
          android:layout_marginTop="@dimen/activity_vertical_margin"
          android:text="@string/title_home" />

  </FrameLayout>

  <android.support.design.widget.BottomNavigationView
      android:id="@+id/navigation"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom"
      android:background="?android:attr/windowBackground"
      android:layout_alignParentBottom="true"
      app:menu="@menu/navigation" />
</RelativeLayout>

如果您将根LinearLayout更改为RelativeLayout,并将参数android:layout_alignParentBottom="true"添加到BottomNavigationView。希望这会有所帮助。

如果您需要更多帮助,可能此链接可以提供帮助:https://medium.com/@hitherejoe/exploring-the-android-design-support-library-bottom-navigation-drawer-548de699e8e0

答案 1 :(得分:0)

顺便说一句,您还可以查看https://github.com/roughike/BottomBar 它是一个自定义视图,如新的Material Design底部导航模式(https://material.io/guidelines/components/bottom-navigation.html#bottom-navigation-behavior)。