2016-07-23 24 views
0

我在UI中有底欄的標籤佈局。我想選擇一個默認選項卡,但不是底部欄。但在我的代碼中,它們都是同時選中的。任何人都可以請告訴我如何解決這個問題?如何創建沒有默認欄的底欄?

+0

包括足夠的代碼讓別人重現問題。有關這方面的幫助,請閱讀http://stackoverflow.com/help/mcve – zajonc

回答

0

你可以做的工作,如果你身邊喜歡

在您添加一個虛擬項目菜單XML

<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:id="@+id/none" 
      android:title="" /> //add this dummy item and set this selected when your view is created 
    <item 
     android:id="@+id/bottomBarItemOne" 
     android:icon="@drawable/ic_recents" 
     android:title="Recents" /> 
    <item 
     android:id="@+id/bottomBarItemtwo" 
     android:icon="@drawable/ic_map" 
     android:title="Map" /> 
    <item 
     android:id="@+id/bottomBarItemthree" 
     android:icon="@drawable/ic_view" 
     android:title="View" /> 

</menu> 

添加您的菜單在活動

public class MainActivity extends AppCompatActivity { 
    private BottomBar mBottomBar; 

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

     mBottomBar = BottomBar.attach(this, savedInstanceState);    
     mBottomBar.setItemsFromMenu(R.menu.menu, new OnMenuTabSelectedListener() { 
      @Override 
      public void onMenuItemSelected(int resId) { 
       if (resId == R.id.bottomBarItemOne) { 
        // the user selected item number one 
       }else if(resId == R.id.bottomBarItemtwo){ 

       } 
      } 
     }); 
     mBottomBar.setDefaultTabPosition(1); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 

     // Necessary to restore the BottomBar's state, otherwise we would 
     // lose the current tab on orientation change. 
     mBottomBar.onSaveInstanceState(outState); 
    } 
} 
+0

即時錯誤setDefaultTabPosition(0); –

+0

不好意思把它改成setDefaultTabPosition(1); – SaravInfern

相關問題