AHBottomNavigation不是全宽

时间:2017-08-03 02:41:51

标签: android bottomnavigationview

我正在从官方底部导航视图切换到this库。 但我注意到使用此库时,宽度与原始库不同 我错过了什么吗?

官方图书馆:

Official Library

AHBottomNavigation:

AHBOttomNavigation

使用AHBottomNavigation布局XML:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mandike.macadamia.views.MainActivity">

    <include
        android:id="@+id/mToolbarMain"
        layout="@layout/toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.aurelhubert.ahbottomnavigation.AHBottomNavigation
        android:id="@+id/bottomNavigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0dp"
        android:background="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_bar_home" />

    <fragment
        android:id="@+id/mFragmentHome"
        class="com.mandike.macadamia.views.HomeFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintTop_toBottomOf="@+id/mToolbarMain"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginBottom="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintHorizontal_bias="0.0"
        android:visibility="gone"/>

    <fragment
        android:id="@+id/mFragmentNotice"
        class="com.mandike.macadamia.views.AnnouncementFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintTop_toBottomOf="@+id/mToolbarMain"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginBottom="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintHorizontal_bias="0.0"
        android:visibility="gone"/>

    <fragment
        android:id="@+id/mFragmentMailbox"
        class="com.mandike.macadamia.views.MailboxFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintTop_toBottomOf="@+id/mToolbarMain"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginBottom="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintHorizontal_bias="0.0"
        android:visibility="gone" />

    <fragment
        android:id="@+id/mFragmentMyAccount"
        class="com.mandike.macadamia.views.MyAccountFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintTop_toBottomOf="@+id/mToolbarMain"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginBottom="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintHorizontal_bias="0.0"
        android:visibility="gone"/>


</android.support.constraint.ConstraintLayout>

使用官方库布局XML(区别仅在库行):

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mandike.macadamia.views.MainActivity">

    <include
        android:id="@+id/mToolbarMain"
        layout="@layout/toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0dp"
        android:background="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_bar_home" />

    <fragment
        android:id="@+id/mFragmentHome"
        class="com.mandike.macadamia.views.HomeFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintTop_toBottomOf="@+id/mToolbarMain"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginBottom="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintHorizontal_bias="0.0"
        android:visibility="gone"/>

    <fragment
        android:id="@+id/mFragmentNotice"
        class="com.mandike.macadamia.views.AnnouncementFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintTop_toBottomOf="@+id/mToolbarMain"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginBottom="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintHorizontal_bias="0.0"
        android:visibility="gone"/>

    <fragment
        android:id="@+id/mFragmentMailbox"
        class="com.mandike.macadamia.views.MailboxFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintTop_toBottomOf="@+id/mToolbarMain"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginBottom="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintHorizontal_bias="0.0"
        android:visibility="gone" />

    <fragment
        android:id="@+id/mFragmentMyAccount"
        class="com.mandike.macadamia.views.MyAccountFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintTop_toBottomOf="@+id/mToolbarMain"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginBottom="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintHorizontal_bias="0.0"
        android:visibility="gone"/>


</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:2)

终于找到了代码。 在AHBottomNavigation.java中,有一些lines可以更改每个项目的宽度

if (titleState == TitleState.ALWAYS_SHOW && items.size() > MIN_ITEMS) {
    minWidth = resources.getDimension(R.dimen.bottom_navigation_small_inactive_min_width);
    maxWidth = resources.getDimension(R.dimen.bottom_navigation_small_inactive_max_width);
}

删除这些行使条形图像官方库。

答案 1 :(得分:1)

检查左侧和右侧设置填充的库中的 bottom_navigation_item.xml link)。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bottom_navigation_container"
    android:layout_width="match_parent"
    android:layout_height="@dimen/bottom_navigation_height"
    android:background="@drawable/item_background"
    android:minWidth="@dimen/bottom_navigation_min_width"
    android:paddingLeft="@dimen/bottom_navigation_padding_left"      // 12dp
    android:paddingRight="@dimen/bottom_navigation_padding_right">   // 12dp

    <ImageView/>

    <TextView/>

    <TextView/>

</FrameLayout>

如果要更改包含项目中的库文件夹并根据您的要求更改值