【问题标题】:BottomNavigationView Is not coming in the bottom after adding the fragment添加片段后,BottomNavigationView 没有进入底部
【发布时间】:2017-04-28 02:33:48
【问题描述】:

我是 android 的初学者,今天我创建了一个 BottomNavigationView 活动,我想用导航按钮显示 3 个不同的选项卡,所以我创建了 3 个片段,问题是添加片段后,BottomNavigationView 显示在顶部side,我应该怎么做,如果我想要底部导航视图就像添加片段之前一样 这是我的主要活动代码

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" />

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    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

    【讨论】:

    • 选择颜色不来
    • 好的,我没有使用return,现在它工作得很好,谢谢兄弟
    【解决方案2】:

    顺便也可以看看https://github.com/roughike/BottomBar 这是一个自定义视图,类似于新的 Material Design 底部导航模式 (https://material.io/guidelines/components/bottom-navigation.html#bottom-navigation-behavior)。

    【讨论】:

      猜你喜欢
      • 2020-10-06
      • 2015-07-24
      • 1970-01-01
      • 2022-10-12
      • 2019-02-09
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多