我正在寻找一种方法来以编程方式更改我的BottomNavigationView.
我找到了如何更改图标颜色,我按如下方式操作:
fun setMenu(selected: Int) {
bottomNavigationMenu?.let {
it.menu.findItem(R.id.bottom_menu_home).icon
.setColorFilter(getMenuColor(R.id.bottom_menu_home, selected), PorterDuff.Mode.SRC_ATOP)
// more items
it.menu.findItem(R.id.bottom_menu_fidelity)?.icon
?.setColorFilter(getMenuColor(R.id.bottom_menu_fidelity, selected), PorterDuff.Mode.SRC_ATOP)
}
}
fun getMenuColor(id: Int, selected: Int): Int {
if (Singletons.restaurant.title.isBlank())
getRestaurant()
else {
if (id == selected) return (Singletons.restaurant.color)
else return ContextCompat.getColor(this, R.color.grey7E)
}
return (0)
}
Run Code Online (Sandbox Code Playgroud)
现在我正在寻找如何更改标题颜色。我正在寻找类似的东西
it.menu.findItem(R.id.bottom_menu_home).title // etc
Run Code Online (Sandbox Code Playgroud)
但我找不到正确的函数来做到这一点。
这是我的main_bottom_menu.xml项目:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/bottom_menu_home"
android:enabled="true"
android:icon="@drawable/ic_home"
android:iconTint="@color/cardview_dark_background"
android:title="@string/bottom_menu_home"
android:visible="false"
app:showAsAction="ifRoom" />
<!-- 3 more items -->
<item
android:id="@+id/bottom_menu_profile"
android:enabled="true" …Run Code Online (Sandbox Code Playgroud) 当使用设计库'com.android.support:design:28.0.0'BottomNavigationView 错误地显示带有长文本的项目标题时:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".mvp.ui.activities.MainActivity">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:layout_marginBottom="56dp"
android:layout_above="@+id/navigation"
android:id="@+id/fragmentContent" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
app:labelVisibilityMode="labeled"
app:itemHorizontalTranslationEnabled="false"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
它看起来很奇怪,因为使用设计库时完全相同的代码com.android.support:design:27.1.1会生成很好的结果:
这怎么可能?对于我尝试使用的新版本的库labelVisibilityMode,但这没有帮助。
app:showAsAction="always"也不行。有任何想法吗?
android navigationbar bottomnavigationview android-bottomnav
我有一个在我的应用程序中实现的底部导航视图。它有 3 个图标:
这就是我的 MapsActivity 的样子
所以我已经成功地显示了片段 A 和 B。但似乎我在 xml 中的某个地方搞乱了片段(我是新人,所以我仍在尝试围绕 xml 的布局进行思考),如果我单击屏幕顶部搜索栏来自 MapsActivity 的位置,然后搜索栏将显示,就像我的片段是“透明”的,但您实际上无法在片段上看到搜索栏。
这就是我的意思:
我如何让我的片段 A 和 B 完全覆盖下面的活动,这样它们就不是“透明的”
活动地图.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/fragmentContainer">
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="@id/place_autocomplete_fragment"
android:layout_above="@id/bottom_navigation"
/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
app:menu="@menu/bottom_nav_menu"
android:background="@color/colorPrimaryDark"
android:layout_width="match_parent"
app:itemIconTint="@color/common_google_signin_btn_text_dark_default"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:layout_marginBottom="0dp"
android:layout_alignParentLeft="true" android:layout_marginLeft="0dp"
android:layout_alignParentStart="true" android:layout_marginStart="0dp"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
片段_a.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="vertical"
android:background="@color/common_google_signin_btn_text_dark_pressed">
<TextView …Run Code Online (Sandbox Code Playgroud) android android-fragments android-activity kotlin bottomnavigationview
我正在尝试向用户显示一个 Snackbar 它是 MainActivity 中的 RideFragment。但是BottomNavigation 与Snackbar 重叠,因此Snackbar 根本不可读/不可见。我看到使用额外的 CoordinatorLayouts 解决了一些类似的问题,但我可以让它工作。
这是 MainActivity 的 XML:
<android.support.design.widget.CoordinatorLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</FrameLayout>
<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:attr/windowBackground"
android:foreground="?attr/selectableItemBackground"
app:itemBackground="@color/colorPrimaryDark"
app:itemHorizontalTranslationEnabled="false"
app:itemIconTint="@android:color/white"
app:itemTextColor="@android:color/white"
app:menu="@menu/navigation" />
Run Code Online (Sandbox Code Playgroud)
有人能帮我吗?
这是 RideFragment 的 XML(用于 Rides 选项卡)
<android.support.constraint.ConstraintLayout 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/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.RidesFragment">
<Button
android:id="@+id/offerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="128dp"
android:text="Offer" …Run Code Online (Sandbox Code Playgroud) 我使用的是普通的底部导航栏,其本地状态包含当前所选页面的索引。当点击导航项之一时,将显示点击的页面:
class _HomePageState extends State<HomePage> {
int _selectedIndex = 1;
final _widgetOptions = [
Text('Index 0'),
Overview(),
Details(),
Text('Index 3'),
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: _widgetOptions.elementAt(_selectedIndex),
bottomNavigationBar: BottomNavigationBar(
...
currentIndex: _selectedIndex,
onTap: onTap: (index) => _selectedIndex = index,
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您想从一个页面内部导航到另一个页面并更新底部导航栏的选定索引,该怎么办?
示例:第 2 页是一个概述列表,其中包含列表项的 ID 等内容。如果您点击列表中的某个项目,它应该导航到详细信息页面(底部导航栏中的 3),显示所选项目的详细信息,因此应传递所选项目的 ID。
Navigator.of(context)...无法使用,因为各个页面是导航栏的项目,因此不是路线。
这与许多问题类似,但我遇到了一些不同的情况。让我先解释一下场景,我有一个带有BottomNavigationView 和FrameLayout 的MainActivity,用于根据BottomNavigation 项目单击切换Fragment。其中一个片段有 FAB & 在这个片段中我需要显示 Snackbar 。
到目前为止我尝试过的内容,
1)在另一个 FrameLayout 中添加了 BottomNavigationView ,但 SnackBar 重叠。
2) 将BottomBar 添加到子CoordinatorLayout 中,并且snackbar 位于Bottombar 后面
这是 MainActivity 和 Fragment 的 XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.HomeActivity">
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:elevation="5dp"
android:background="@android:color/white"
app:itemBackground="@android:color/white"
android:foreground="?attr/selectableItemBackground"
app:labelVisibilityMode="labeled"
app:itemTextColor="@color/darkestGrey"
app:menu="@menu/navigation" />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
这是片段布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.PayloadsFragment">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<FrameLayout
android:id="@+id/toolbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar …Run Code Online (Sandbox Code Playgroud) android android-layout android-fragments android-snackbar bottomnavigationview
我正在使用 android 导航组件开发 android,但我收到以下警告以下任何函数都不能使用提供的参数调用。androidx.navigation.ui.NavigationUI 中定义的 navigateUp(NavController, DrawerLayout?) androidx.navigation.ui.Navigation 中定义的 navigateUp(NavController, AppBarConfiguration)
在我的 MainActivity.kt 下面
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.setupWithNavController
import com.example.forecastmvvm.R
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
navController = Navigation.findNavController(this,R.id.nav_host_fragment)
bottom_nav.setupWithNavController(navController)
NavigationUI.setupActionBarWithNavController(this, navController)
}
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp( null, navController)
}
}
Run Code Online (Sandbox Code Playgroud)
在 app.gradle 下面
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: "kotlin-kapt"
apply plugin: …Run Code Online (Sandbox Code Playgroud) 我有一个CoordinatorLayout包含一个BottomNavigationView和AppBarLayout一个ToolBar里面有一个的。(BottomNavigationView不在 内AppBarLayout,因为这样做会破坏 的位置BottomNavigationView)。
我需要以编程方式显示/隐藏AppBarLayout和BottomNavigationView,当某些事件发生时(例如在片段上onResume),到目前为止我已经管理设置appBarLayout.setExpanded(true, true)以显示/隐藏 AppBar,但我不知道如何做相同BottomNavigationView,因为它没有任何显示/隐藏自身的方法。
我的行为BottomNavigationView是app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior",在布局 xml 中设置。如何在我的代码中获得对这种行为的引用以管理其展开/折叠状态?
创建片段时,我从网络下载数据。单击已激活的菜单项时如何避免重新加载?转换与导航组件一起使用。我知道我需要听取媒体的意见,但我还没有弄清楚实施方式。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navController = findNavController(R.id.fragmentContainerView)
val appBarConfiguration = AppBarConfiguration(setOf(R.id.ruleListFragment))
setupActionBarWithNavController(navController, appBarConfiguration)
bottomNavigatinView.setupWithNavController(navController)
bottomNavigatinView.setOnNavigationItemSelectedListener {
//TODO
}
}
}
Run Code Online (Sandbox Code Playgroud) \xe3\x80\x90update\xe3\x80\x91\n实际上,只需禁用默认的 ActionBar 并为每个片段添加工具栏即可轻松重现该问题。然后您可以看到切换底部目标时工具栏会闪烁。\n
\n请查看https://github.com/025nju/BottomNav上的小演示
\xe3\x80\x90原始帖子\xe3\x80\x91\n我是 Android 新手,我的演示项目是 BottomNav 单活动项目。我检查了很多帖子,看起来很多人都在这种情况下处理工具栏,所有片段都需要有自己的工具栏。
\n在底部选项卡之间切换时,工具栏背景颜色会闪烁。请查看 gif 并忽略最后一个空白帧。\n
如何消除闪烁?\nbtw,当我使用默认的ActionBar时没有出现这样的问题,这是官方不推荐的。\n我在Nexus 6 api26 AVD和真机android版本9上进行了测试。同样的问题。下面是相关代码供大家参考。多谢。
\nFragmentMoney.xml:
\n<?xml version="1.0" encoding="utf-8"?>\n<layout xmlns:android="http://schemas.android.com/apk/res/android"\n xmlns:app="http://schemas.android.com/apk/res-auto"\n xmlns:tools="http://schemas.android.com/tools">\n<data>\n</data>\n<androidx.constraintlayout.widget.ConstraintLayout\n android:layout_width="match_parent"\n android:layout_height="match_parent"\n tools:context=".ui.money.MoneyFragment">\n\n <include android:id="@+id/fragment_base" layout="@layout/fragment_base"/>\n\n <TextView\n android:id="@+id/text_money"\n ... />\n</androidx.constraintlayout.widget.ConstraintLayout>\nRun Code Online (Sandbox Code Playgroud)\n\nfragment_base.xml 用于工具栏定义:
\n<?xml version="1.0" encoding="utf-8"?>\n<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"\n xmlns:app="http://schemas.android.com/apk/res-auto"\n android:layout_width="match_parent"\n android:layout_height="match_parent">\n<com.google.android.material.appbar.AppBarLayout\n android:layout_width="match_parent"\n android:layout_height="wrap_content">\n\n <com.google.android.material.appbar.MaterialToolbar\n android:id="@+id/mToolbar"\n android:layout_width="match_parent"\n android:layout_height="?attr/actionBarSize"\n android:background="?attr/colorPrimary"\n android:elevation="4dp"\n android:theme="@style/ThemeOverlay.AppCompat.ActionBar"\n app:popupTheme="@style/ThemeOverlay.AppCompat.Light"\n app:layout_constraintTop_toTopOf="parent">\n </com.google.android.material.appbar.MaterialToolbar>\n\n</com.google.android.material.appbar.AppBarLayout>\n\n</androidx.coordinatorlayout.widget.CoordinatorLayout>\nRun Code Online (Sandbox Code Playgroud)\nMoneyFragment.java:
\npublic View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {\n View root = fragmentMoneyBinding.getRoot();\n ...\n …Run Code Online (Sandbox Code Playgroud) android-fragments android-toolbar bottomnavigationview android-jetpack