bottomappbar Android中的导航抽屉

时间:2018-09-27 05:40:58

标签: android navigation-drawer

enter image description here

class BottomNavigationDrawerFragment: BottomSheetDialogFragment(), 
   NavigationView.OnNavigationItemSelectedListener {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_bottomsheet, container, false)
    }

    override fun onNavigationItemSelected(item : MenuItem): Boolean {
        // Bottom Navigation Drawer menu item clicks
        when (item.itemId) {
            R.id.nav1 -> context!!.toast("oneeeeee")
            R.id.nav2 -> context!!.toast("twoooooo")
            R.id.nav3 -> context!!.toast("threeeee")

            return true
        }
        // Add code here to update the UI based on the item selected
        // For example, swap
    }
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        navigation_view.setNavigationItemSelectedListener(this)

        // Add code here to update the UI based on the item selected
        // For example, swap
    }
}

// This is an extension method for easy Toast call
fun Context.toast(message: CharSequence) {
    val toast = Toast.makeText(this, message, Toast.LENGTH_SHORT)
    toast.setGravity(Gravity.BOTTOM, 0, 600)
    toast.show()
}

我要实现的是图像中给出的内容。我想在底部的应用栏中创建一个navigation drawer。上面的代码不起作用,它告诉了未解析的引用类型setNavigationItemSelectedListener。我的代码有什么错误?

3 个答案:

答案 0 :(得分:0)

查看此代码,它具有navigationIcon属性,但是您可以将其用作底部的应用栏。如果您需要单击导航栏,则必须由我们自己进行定制。

document.getElementById('time').addEventListener('click', function(e) {
    e.preventDefault();
}

在res> menu> bottom_bar_menu中,将showAsAction更改为always或ifRoom,放置一个用于action_settings的图标并删除orderInCategory

<com.google.android.material.bottomappbar.BottomAppBar
  android:id="@+id/bottom_app_bar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="bottom"
  app:elevation="5dp"
  android:elevation="5dp"
  app:fabAttached="true"
  app:fabCradleDiameter="0dp"
  app:backgroundTint="@color/colorPrimary"
  app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
  app:fabAlignmentMode="center"
  app:menu="@menu/bottom_bar_menu"/>

在Java中:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:showAsAction="always"
        android:icon="" />
    <item
        android:title="@string/search"
        android:id="@+id/search"
        android:icon="@drawable/ic_search_black_24dp"
        android:showAsAction="always" />

    <item
        android:id="@+id/app_bar_archieve"
        android:icon="@drawable/ic_bottom_bar_hamburger" // navigation icon
        android:title="@string/action_archieve"
        app:showAsAction="ifRoom"/>
</menu>

参考链接:https://material.io/develop/android/components/bottom-app-bar/

答案 1 :(得分:0)

您应该在bottomAppbar中添加一个抽屉图标,然后使用bottomsheet作为抽屉。

对于抽屉,您有两种选择:

1 -使用Google标准,并在菜单文件夹中添加抽屉项(似乎您不希望这样做)

2 -替换底部的一个片段,这样您就可以自定义片段并做任何您想做的事

------------------- 在底页中替换一个片段 ------------- < / p>

您的activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:custom="http://schemas.android.com/tools"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layoutDirection="ltr"
        android:background="@color/white"
        android:orientation="vertical">

            <FrameLayout
                android:id="@+id/bottom_sheet"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false"
                android:elevation="6dp"
                android:visibility="visible"
                app:layout_behavior="@string/bottom_sheet_behavior">

                 <FrameLayout
                    android:id="@+id/menu"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" />
            </FrameLayout>

  </androidx.coordinatorlayout.widget.CoordinatorLayout>

您的Activity.java

public class Activity extends AppCompatActivity implements 
FragmentNavigation.OnFragmentInteractionListener {
    private CoordinatorLayout coordinatorLayout;
    private View bottomSheet;
    private BottomSheetBehavior<View> behavior;

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

        FrameLayout bottomSheetLayout = (FrameLayout) 
        findViewById(R.id.menu);

        FragmentNavigation fragmentNavigation = new FragmentNavigation();

        androidx.fragment.app.FragmentTransaction fragmentTransaction = 
        getSupportFragmentManager().beginTransaction();

        fragmentTransaction.replace(bottomSheetLayout.getId(), 
        fragmentNavigation, "k");

        fragmentTransaction.commit();


        coordinatorLayout = (CoordinatorLayout) 
        findViewById(R.id.main_content);
        bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);
        behavior = BottomSheetBehavior.from(bottomSheet);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}

您的片段导航

    public class FragmentNavigation extends androidx.fragment.app.Fragment {
    private String descriptions;

    public FragmentNavigation () {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view = inflater.inflate(R.layout.fragment_navigation, container, false);

        return view;
    }
}

您的fragment_navigation.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:custom="http://schemas.android.com/tools"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical">

                <TextView
                    android:id="@+id/bottom_sheet"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:clipToPadding="false"
                    android:elevation="6dp"
                    android:visibility="visible"
                    android:text="here is the navigation menu"
                    app:layout_behavior="@string/bottom_sheet_behavior"/>



      </LinearLayout>

答案 2 :(得分:0)

bottom_bar.replaceMenu(R.menu.bottomappbar_menu)
    bottom_bar.setOnMenuItemClickListener {
        when (it.itemId) {
            R.id.app_bar_copy -> {

            }
            R.id.app_bar_fav -> {

            }
            R.id.app_bar_tra -> {

            }
            else -> {

            }
        }
         true
    }

只需在片段中添加代码即可处理菜单项。