2017-04-06 46 views
3

我是Android的初学者,今天我创建了一个BottomNavigationView活动,我想用导航按钮显示3个不同的选项卡,所以我创建了3个片段,问题发生在加入的时候BottomNavigationView在顶侧like this显示片段,我应该怎么做,如果我在底部要BottomNavigationView像它添加片段 之前,这里是我的主要活动代码BottomNavigationView在添加片段后未进入底部

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

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> 

如果改变了你的根LinearLayoutRelativeLayout并添加参数android:layout_alignParentBottom="true"BottomNavigationView。希望这会有所帮助。

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

+0

选择颜色不会来 –

+0

好吧我得到了,我没有使用回报,现在它的工作很好,谢谢你的兄弟 –

+0

不客气。祝你的程序好运。 – Neoklosch