如何增加BottomNavigationView的宽度以填充屏幕 [英] How to increase width of BottomNavigationView to fill the screen
问题描述
如何增加BottomNavigationView菜单的宽度.以下是当前方案.我无法增加宽度以完成屏幕.
Menu.xml
<?xml version ="1.0" encoding ="utf-8"?>< RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"xmlns:tools ="http://schemas.android.com/tools"xmlns:app ="http://schemas.android.com/apk/res-auto"android:layout_width ="match_parent"android:layout_height ="match_parent"工具:context ="com.idea.nikhil.googlemaps.bottomNavigationbar">< FrameLayoutandroid:id ="@ + id/container"android:layout_width ="match_parent"android:layout_height ="match_parent"android:layout_above ="@ + id/bottom_navigation"android:layout_alignParentTop ="true"></FrameLayout>< android.support.design.widget.BottomNavigationViewandroid:id ="@ + id/bottom_navigation"android:layout_width ="match_parent"android:layout_height ="wrap_content"android:background ="@ color/white"app:itemIconTint ="@ drawable/bottom_navigation_selector"app:itemTextColor ="@ drawable/bottom_navigation_selector"app:menu ="@ menu/bottom_navigation_menu"android:layout_alignParentBottom ="true"/></RelativeLayout>
这是可行的(即使没有反思).但这会再次
这不是魔术.
为什么添加了具有这样名称和尺寸的尺寸为600dp的尺寸
BottomNavigationMenuView.java
(这是用于表示 BottomNavigationView
中的菜单的类)都使用这两个尺寸.下面是代码
public BottomNavigationMenuView(Context context,AttributeSet attrs){超级(上下文,attrs);...mInactiveItemMaxWidth = res.getDimensionPixelSize(R.dimen.design_bottom_navigation_item_max_width);....mActiveItemMaxWidth = res.getDimensionPixelSize(R.dimen.design_bottom_navigation_active_item_max_width);.....}
现在这些值将用于创建固定宽度的视图,如下所示
@Override受保护的void onMeasure(int widthMeasureSpec,int heightMeasureSpec){....如果(mShiftingMode){最后的int inactiveCount =计数-1;最后的int activeMaxAvailable = width-inactiveCount * mInactiveItemMinWidth;final int activeWidth = Math.min(activeMaxAvailable,mActiveItemMaxWidth);最后的int inactiveMaxAvailable =(width-activeWidth)/inactiveCount;最后的int inactiveWidth = Math.min(inactiveMaxAvailable,mInactiveItemMaxWidth);...} 别的 {final int maxAvailable =宽度/计数;final int childWidth = Math.min(maxAvailable,mActiveItemMaxWidth);.....}....}
要始终使用 activeMaxAvailable
的值,我将一个哑数值设置为 mActiveItemMaxWidth
(在上面的阴影中).因此, activeWidth
将具有 activeMaxAvailable
的值.相同的规则适用于 inactiveWidth
.
因此,在构建项目时,定义了 design_bottom_navigation_item_max_width
和 design_bottom_navigation_active_item_max_width
放入 design-support-lib 中,将替换为我们定义的尺寸.
还验证了支持的最大选项(最多5个)上的代码.
How to increase width of BottomNavigationView menu. Below is the current scenario. I am not able to increase the width to complete screen.
Menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.idea.nikhil.googlemaps.bottomNavigationbar">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_navigation"
android:layout_alignParentTop="true">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:itemIconTint="@drawable/bottom_navigation_selector"
app:itemTextColor="@drawable/bottom_navigation_selector"
app:menu="@menu/bottom_navigation_menu"
android:layout_alignParentBottom="true" />
</RelativeLayout>
That is doable (Even without reflection). But it will againts default design specs, and I wil suggest you to go with default design specs.
Now coming to my solution...
Define below dimensions into dimens.xml
. These dimensions should be in values
, values-large
, values-large-land
and 600dp
can be increased to 1000dp
or more in values-large
, values-large-land
, if in tablet you are not seeing this change.
<resources xmlns:tools="http://schemas.android.com/tools">
<dimen name="design_bottom_navigation_item_max_width" tools:override="true">600dp</dimen>
<dimen name="design_bottom_navigation_active_item_max_width" tools:override="true">600dp</dimen>
</resources>
And thats it!!! Result will be like
It's not a mazic.
Why dimensions has been added with such name and value is 600dp
Both dimensions is being used by BottomNavigationMenuView.java
(which is the class being used to represent menu in BottomNavigationView
).
Below is code
public BottomNavigationMenuView(Context context, AttributeSet attrs) {
super(context, attrs);
...
mInactiveItemMaxWidth = res.getDimensionPixelSize(
R.dimen.design_bottom_navigation_item_max_width);
....
mActiveItemMaxWidth = res.getDimensionPixelSize(
R.dimen.design_bottom_navigation_active_item_max_width);
.....
}
Now these values is being used to create view with fixed width, as below
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
....
if (mShiftingMode) {
final int inactiveCount = count - 1;
final int activeMaxAvailable = width - inactiveCount * mInactiveItemMinWidth;
final int activeWidth = Math.min(activeMaxAvailable, mActiveItemMaxWidth);
final int inactiveMaxAvailable = (width - activeWidth) / inactiveCount;
final int inactiveWidth = Math.min(inactiveMaxAvailable, mInactiveItemMaxWidth);
...
} else {
final int maxAvailable = width / count;
final int childWidth = Math.min(maxAvailable, mActiveItemMaxWidth);
.....
}
....
}
To use value of activeMaxAvailable
always, I set a dummy value to mActiveItemMaxWidth
(in dimens above). So activeWidth
will have
value of activeMaxAvailable
. Same rule apply for inactiveWidth
.
So when you build project, design_bottom_navigation_item_max_width
and design_bottom_navigation_active_item_max_width
defined
into design-support-lib, will be replaced by dimensions defined by us.
Code verified on maximum supported options (5 max) also.
这篇关于如何增加BottomNavigationView的宽度以填充屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!