我正在尝试使用设计库中的 BottomNavigationView。一切正常,除了我希望每个导航项目都开始一个活动,因此我想取消选中导航中的所有项目,使它们看起来相同。我尝试了几种解决方案,其中大部分都不起作用,最后一个确实有效,但感觉很hacky。
首先我这样做:
ViewGroup nav = (ViewGroup) bottomNav;
for(int i=0; i < nav.getChildCount(); i++) {
nav.getChildAt(i).setSelected(false);
}
Run Code Online (Sandbox Code Playgroud)
这似乎什么也没做。
然后我尝试:
int size = bottomNav.getMenu().size();
for (int i = 0; i < size; i++) {
bottomNav.getMenu().getItem(i).setChecked(false);
}
Run Code Online (Sandbox Code Playgroud)
这仅检查了最后一项而不是第一项。
最后我尝试在菜单中添加一个虚拟项目并执行以下操作:
bottomNav.getMenu().findItem(R.id.dummmy_item).setChecked(true);
bottomNav.findViewById(R.id.dummmy_item).setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)
这几乎有效,但它隐藏了下面的标题,在我的情况下这对上下文很重要。
然后我找到了这个答案:https : //stackoverflow.com/a/41372325/4888701并编辑了我上面的解决方案以包含它。具体来说,我添加了 proguard 规则,我使用了那个确切的帮助器类并调用了该方法。它看起来正确,似乎有效。但对我来说感觉很hacky,因为:
有没有其他更简单的方法来实现这一点,或者这是我们当前版本的库中最好的方法?
(作为旁注,我想知道此解决方案中的 proguard 规则是否必要以及它的作用是什么?我对 proguard 一无所知,但该项目是从启用它的其他人那里继承的。)
我是android的新手,我使用带有两个菜单的底部导航视图
但我得到了正确的空间
这是我的导航视图,
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorRed"
app:itemIconTint="@color/colorWhite"
app:itemTextColor="@color/colorWhite"
app:menu="@menu/bottom_bar_menu"/>
Run Code Online (Sandbox Code Playgroud)
我有一个底部导航视图的设置。我加载的 3 个片段之一有一个带有另外 3 个片段的标签栏。我确实在底部导航点击时替换了这样的片段:
private void loadPage(int page)
{
if(fragmentManager == null)
fragmentManager = getSupportFragmentManager();
final FragmentTransaction transaction = fragmentManager.beginTransaction();
//transaction.setCustomAnimations(R.anim.fragment_fade_in, R.anim.fragment_fade_out, R.anim.fragment_fade_in, R.anim.fragment_fade_out);
//transaction.setCustomAnimations(R.anim.fragment_fade_in, R.anim.fragment_fade_out, R.anim.fragment_fade_in, R.anim.fragment_fade_out);
switch (page)
{
//Property
case TAB_PROPERTIES:
Fragment fragment = fragmentManager.findFragmentByTag("property");
if(fragment == null)
fragment = PropertyFragment.newInstance();
transaction.replace(R.id.rlMainContent, fragment,"property").addToBackStack("property").commit();
break;
//jobs
case TAB_JOBS:
fragment = fragmentManager.findFragmentByTag("jobs");
if(fragment == null)
fragment = JobsFragment.newInstance(null);
transaction.replace(R.id.rlMainContent, fragment,"jobs").addToBackStack("jobs").commit();
break;
//contacts
case TAB_CONTACTS:
fragment = fragmentManager.findFragmentByTag("contacts");
if(fragment == null)
fragment = PersonalContactsFragment.newInstance(false,true,false,true);
transaction.replace(R.id.rlMainContent, fragment,"contacts").addToBackStack("contacts").commit();
break;
}
} …Run Code Online (Sandbox Code Playgroud) 布局:
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:onNavigationItemSelected="@{viewModel.onNavigationItemSelected}"
android:background="?android:attr/windowBackground"
app:menu="@menu/menu_home_tab" />
Run Code Online (Sandbox Code Playgroud)
代码:
@BindingAdapter("onNavigationItemSelected")
public static void setOnNavigationItemSelected(
BottomNavigationView view, BottomNavigationView listener) {
view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_dashboard:
Log.d("test","test1");
return true;
case R.id.navigation_notifications:
Log.d("test","test2");
return true;
}
return false;
}
});
}
Run Code Online (Sandbox Code Playgroud)
这将返回一个错误
Error:(183, 49) Could not find accessor viewmodel.onNavigationItemSelected
Run Code Online (Sandbox Code Playgroud)
我正在尝试在我的底部导航视图上实现数据绑定
android mvvm android-layout android-databinding bottomnavigationview
我正在使用带有 5 个片段的导航底部。
每个片段都会进行 HTTP 调用并下载内容,但是在替换片段时,视图会被破坏并再次下载。
在我的搜索中,我发现我应该使用 setOffscreenPageLimit 这个方法,但我不使用 viewpager。
我怎么能实现这样的东西?
navigation android android-fragments android-volley bottomnavigationview
我正在尝试实现一个持久的BottomSheet,我layout期望是一个BottomSheet已经是的孩子,CoordinatorLayout但我不知道为什么我得到这个异常。
这是我的BottomSheet布局:
<?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:tools="http://schemas.android.com/tools"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical"
android:padding="10dp"
app:behavior_hideable="true"
app:behavior_peekHeight="?actionBarSize"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:orientation="horizontal"
android:layout_gravity="center_vertical"
android:weightSum="3">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_weight="2"
android:text="Order Details"
android:textColor="#444"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:gravity="right"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="15dp"
android:text="?435.00"></TextView>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chicken Fried Rice 1x1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paneer Tikka 1x2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Delivery Address"
android:textColor="#444"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content" …Run Code Online (Sandbox Code Playgroud) 我想在大写中制作 BottomNavigationView 菜单,如何在不使用 3rd 方库的情况下做到这一点?
这是我的 xml 代码:
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@android:color/white"
android:foreground="?attr/selectableItemBackground"
app:itemIconTint="@color/bottom_nav_color"
app:itemTextColor="@color/bottom_nav_color"
app:menu="@menu/navigation" />
Run Code Online (Sandbox Code Playgroud)
和 navigation.xml 如下:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/home_icon"
android:title="@string/title_home"
/>
<item
android:id="@+id/navigation_deals"
android:icon="@drawable/ic_deals"
android:title="@string/deals" />
<item
android:id="@+id/navigation_myBookings"
android:icon="@drawable/ic_my_bookings"
android:title="@string/title_my_bookings" />
<item
android:id="@+id/navigation_more"
android:icon="@drawable/ic_more"
android:title="@string/title_more" />
</menu>
Run Code Online (Sandbox Code Playgroud) 如何在BottomNavigationView中的选定项目上应用文本样式?我可以更改颜色,但无法弄清楚如何更改所选菜单项上的字体样式(如字体系列或使其加粗/斜体)。有没有办法只用 XML 来做到这一点?
在上面的示例图片中,我只希望 SEARCH 使用不同的字体和粗体。MESSAGE 和 DASHBOARD 保持不变。
在活动 xml 中:
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="49dp"
android:background="?android:attr/windowBackground"
android:paddingBottom="5dp"
android:paddingTop="5dp"
app:itemIconTint="@color/bottom_nav_color"
app:itemTextColor="@color/bottom_nav_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation"/>
Run Code Online (Sandbox Code Playgroud)
res/menu/navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_search"
android:icon="@drawable/ic_search"
android:title="SEARCH" />
<item
android:id="@+id/navigation_messages"
android:icon="@drawable/ic_question_answer"
android:title="MESSAGES" />
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="DASHBOARD" />
</menu>
Run Code Online (Sandbox Code Playgroud)
res/color/bottom_nav_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/colorPrimary" />
<item android:state_checked="false" android:color="#666666"/>
</selector>
Run Code Online (Sandbox Code Playgroud) 我想要做的是我的 Android 项目中的底部导航视图的行为类似于 IOS 视图。当我将 Iphone 旋转到横向时,栏的高度较小,图标位于文本的一侧。
当手机处于垂直位置时,文本和图标应该堆叠。
当我旋转到横向时,文本和图标应该像这样水平显示。
这些图片来自材料文档:https :
//material.io/design/components/bottom-navigation.html#placement
但是当我旋转到横向时,我找不到如何做第二个选项。有谁知道?
谢谢!
我正在尝试使用 BottomNavigationView 在 3 个 Fragment 之间移动,我让它工作了,但我有一个小问题。
我有 2 个片段和 1 个活动,这个活动的代码在其中的片段之间移动。
当我按下底部导航栏上的按钮时,我可以看到底部栏变化的图像。
在一个片段中,我制作了一个按钮,按下时会移动到另一个片段,而且效果很好。
我面临的问题是,当我按下按钮移动到另一个片段时,底栏图像不会改变。
这是我的主要活动代码:
public class MainActivity extends AppCompatActivity {
public static Bundle bundle = new Bundle();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_announcement:
selectedFragment = new AnnouncementFragment(); …Run Code Online (Sandbox Code Playgroud) android android-layout android-fragments bottomnavigationview