从 android 支持设计导航抽屉中移除滚动条?
Remove scrollbar from android support design navigation drawer?
我刚刚将支持设计库从 22.2.1 更新到 23.0.1,并立即注意到导航抽屉中存在滚动条。我尝试使用
android:scrollbars="none"
但这并没有解决问题。有没有其他方法可以去掉滚动条?
不幸的是,滚动条设置在 NavigationMenuView
布局中而不是 NavigationView
,因此如果您使用 android:scrollbars="none"
,滚动条仍然存在。
您可以调用此方法以编程方式执行此操作:
private void disableNavigationViewScrollbars(NavigationView navigationView) {
if (navigationView != null) {
NavigationMenuView navigationMenuView = (NavigationMenuView) navigationView.getChildAt(0);
if (navigationMenuView != null) {
navigationMenuView.setVerticalScrollBarEnabled(false);
}
}
}
您还可以在 style.xml
中使用以下样式
<item name="android:scrollbarThumbVertical">@color/transparent</item>
if you don't have a transparent color defined in colors.xml, use the android library "transparent" with: <item name="android:scrollbarThumbVertical">@android:color/transparent</item>
我尝试在 Kotlin 中这样做,希望它会有所帮助。
首先,创建不同的片段和您想要的任何导航,然后创建一个函数,该函数将用于将片段加载到 activity.
when (item.getItemId()) {
R.id.home -> {
//this is the name of the method I am using for adding fragments
//with bottom navigation bar you can use it with any type o navigation.
loadFragment(getString(R.string.home_fragment), HomeFragment());
appBar.title = "Home"
return true
}
R.id.jobs -> {
loadFragment(getString(R.string.jobs_fragment), JobsFragment());
appBar.title = "Jobs"
return true
}
之后就是方法
private fun loadFragment(tag: String,loadFragment: Fragment) {
val fManager = supportFragmentManager
val fTransaction = fManager.beginTransaction()
val fragment = fManager.findFragmentByTag(tag)
if (fragment == null) {
fTransaction.replace(R.id.activity_main_content_main, loadFragment,tag);
} else { // re-use the old fragment
fTransaction.replace(R.id.activity_main_content_main, fragment, tag);
}
fTransaction.addToBackStack(tag);
fTransaction.commit();
}
first val fragment = fManager.findFragmentByTag(tag) 这将搜索片段是否已经加载,然后将执行 else 语句并显示预加载的片段,但如果没有
然后 loadFragment 我们传递的参数包含您要加载的片段,然后将执行 if 语句,这将加载传递的片段。
您可以在 apptheme 中使用它的风格:
<item name="android:scrollbarThumbVertical">@android:color/transparent</item>
我刚刚将支持设计库从 22.2.1 更新到 23.0.1,并立即注意到导航抽屉中存在滚动条。我尝试使用
android:scrollbars="none"
但这并没有解决问题。有没有其他方法可以去掉滚动条?
不幸的是,滚动条设置在 NavigationMenuView
布局中而不是 NavigationView
,因此如果您使用 android:scrollbars="none"
,滚动条仍然存在。
您可以调用此方法以编程方式执行此操作:
private void disableNavigationViewScrollbars(NavigationView navigationView) {
if (navigationView != null) {
NavigationMenuView navigationMenuView = (NavigationMenuView) navigationView.getChildAt(0);
if (navigationMenuView != null) {
navigationMenuView.setVerticalScrollBarEnabled(false);
}
}
}
您还可以在 style.xml
中使用以下样式<item name="android:scrollbarThumbVertical">@color/transparent</item>
if you don't have a transparent color defined in colors.xml, use the android library "transparent" with: <item name="android:scrollbarThumbVertical">@android:color/transparent</item>
我尝试在 Kotlin 中这样做,希望它会有所帮助。 首先,创建不同的片段和您想要的任何导航,然后创建一个函数,该函数将用于将片段加载到 activity.
when (item.getItemId()) {
R.id.home -> {
//this is the name of the method I am using for adding fragments
//with bottom navigation bar you can use it with any type o navigation.
loadFragment(getString(R.string.home_fragment), HomeFragment());
appBar.title = "Home"
return true
}
R.id.jobs -> {
loadFragment(getString(R.string.jobs_fragment), JobsFragment());
appBar.title = "Jobs"
return true
}
之后就是方法
private fun loadFragment(tag: String,loadFragment: Fragment) {
val fManager = supportFragmentManager
val fTransaction = fManager.beginTransaction()
val fragment = fManager.findFragmentByTag(tag)
if (fragment == null) {
fTransaction.replace(R.id.activity_main_content_main, loadFragment,tag);
} else { // re-use the old fragment
fTransaction.replace(R.id.activity_main_content_main, fragment, tag);
}
fTransaction.addToBackStack(tag);
fTransaction.commit();
}
first val fragment = fManager.findFragmentByTag(tag) 这将搜索片段是否已经加载,然后将执行 else 语句并显示预加载的片段,但如果没有 然后 loadFragment 我们传递的参数包含您要加载的片段,然后将执行 if 语句,这将加载传递的片段。
您可以在 apptheme 中使用它的风格:
<item name="android:scrollbarThumbVertical">@android:color/transparent</item>