如何创建一个没有选择默认栏的底栏?

时间:2016-07-23 07:54:22

标签: android android-tablayout

我在UI中有底栏的标签布局。我想选择默认选项卡,但不选择底栏。但在我的代码中,它们都是同时被选中的。有谁能告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分: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);
    }
}