[英]Navigation Drawer in bottomappbar android
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
。 我的代碼中有什么錯誤?
請參閱此代碼,它具有navigationIcon屬性,但您可以將其用作底部應用欄。 如果您在此點擊時需要導航抽屜,則必須由我們自己定制。
<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"/>
在res> menu> bottom_bar_menu中,將showAsAction更改為always或ifRoom,為action_settings添加一個圖標並刪除orderInCategory
<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>
在java中:
BottomAppBar bar = (BottomAppBar) findViewById(R.id.bar);
setSupportActionBar(bar);
bar.setNavigationOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Handle the navigation click by showing a BottomDrawer etc.
}
});
bar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// Handle actions based on the menu item
return true;
}
});
參考鏈接: https : //material.io/develop/android/components/bottom-app-bar/
你應該在bottomAppbar
添加一個抽屜圖標,然后使用bottomsheet
的抽屜bottomsheet
。
對於您的抽屜,您有兩個選擇:
1 - 使用谷歌標准並在菜單文件夾中添加抽屜項目(似乎你不想要這個)
2 - 替換底頁中的片段,這樣您就可以自定義片段並執行任何操作
------------------- 替換底頁中的片段 -------------
你的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>
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
}
只需在片段中添加代碼即可處理菜單項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.