2017-04-06 21 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