gpt4 book ai didi

android - bottomNavigationView 目的地可以水平放置而不是堆叠

转载 作者:太空狗 更新时间:2023-10-29 13:03:31 26 4
gpt4 key购买 nike

我想要做的是让我的 Android 项目中的 BottomNavigation View 表现得像 IOS 一样。当我将 Iphone 旋转到横向时,栏的高度较小,图标位于文本的一侧。

当手机处于垂直位置时,文本和图标应堆叠。

enter image description here当我旋转到横向时,文本和图标应该像这样水平显示。 enter image description here这些图片来自 Material 文档: https://material.io/design/components/bottom-navigation.html#placement

但是当我旋转到横向时,我找不到如何执行第二个选项。有人知道吗?

谢谢!

最佳答案

我已经通过创建自定义布局解决了这个问题。item_bottom_navigation_bar.xml 示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:paddingTop="5dp">

<ImageView
android:id="@+id/icon"
android:layout_margin="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

然后我将它添加到我的 BottomNavigationView 的每个项目中:

LayoutInflater inflater = getLayoutInflater();
BottomNavigationMenuView navigationMenuView = (BottomNavigationMenuView) mBinding.bottomNavigation.getChildAt(0);
Menu menu = mBinding.bottomNavigation.getMenu();
for (int i = 0; i < menu.size(); i++) {
BottomNavigationItemView view = (BottomNavigationItemView) navigationMenuView.getChildAt(i);
View itemBottomNavigation = inflater.inflate(R.layout.item_bottom_navigation_bar, null, false);
((ImageView) itemBottomNavigation.findViewById(R.id.icon)).setImageDrawable(menu.getItem(i).getIcon());
((TextView) itemBottomNavigation.findViewById(R.id.title)).setText(menu.getItem(i).getTitle());
view.removeAllViews();
view.addView(itemBottomNavigation);
}

mBinding.bottomNavigation 是您的 View (可能是 findViewById(R.id.bottomNavigationView) )

希望对您有所帮助!

关于android - bottomNavigationView 目的地可以水平放置而不是堆叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52152721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com