简体   繁体   中英

Change alignment of icon and text in custom bottom navigation bar in android

I am using "com.luseen.spacenavigation.SpaceNavigationView" for my custom BottomNavigation bar. It works fine but my issue is with alignment of the icon and text in the bottom navigation bar. It aligned horizontally , but i want text to align below the icon. How can I change the alignment?

My XML

<com.luseen.spacenavigation.SpaceNavigationView
    android:id="@+id/space"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    app:active_item_color="@color/colorAccent"
    app:centre_button_color="@android:color/holo_green_light"
    app:inactive_item_color="@android:color/darker_gray"
    app:space_background_color="@android:color/holo_green_light"
    app:centre_button_icon="@drawable/img1"
    app:space_item_icon_size="@dimen/space_item_icon_default_size"
    app:space_item_icon_only_size="@dimen/space_item_icon_only_size"
    app:space_item_text_size="@dimen/space_item_text_default_size"
    app:layout_behavior="com.luseen.spacenavigation.SpaceNavigationViewBehavior"/>

In my Activity

 spaceNavigationView = (SpaceNavigationView) findViewById(R.id.space);
    spaceNavigationView.initWithSaveInstanceState(savedInstanceState);
    spaceNavigationView.addSpaceItem(new SpaceItem("Home", R.drawable.account));
    spaceNavigationView.addSpaceItem(new SpaceItem("Help", R.drawable.ic_home_black_24dp));
    spaceNavigationView.addSpaceItem(new SpaceItem("Bookings", R.drawable.ic_dashboard_black_24dp));
    spaceNavigationView.addSpaceItem(new SpaceItem("Settings", R.drawable.ic_notifications_black_24dp));
    spaceNavigationView.shouldShowFullBadgeText(true);
    spaceNavigationView.setCentreButtonIconColorFilterEnabled(false);
    spaceNavigationView.setSpaceBackgroundColor(Color.WHITE);
    spaceNavigationView.setBackgroundColor(Color.GREEN);

you have to edit the space_item_view.xml layout file, inside the library to solve this problem.

space_item_view.xml

    <RelativeLayout
    android:id="@+id/main_content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/space_icon"
        android:layout_width="@dimen/space_item_icon_default_size"
        android:layout_gravity="center"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_height="@dimen/space_item_icon_default_size" />

    <TextView
        android:id="@+id/space_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:lines="1"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/space_icon"
        android:gravity="center"
        android:textSize="@dimen/space_item_text_default_size"
        tools:text="Label One" />
    </RelativeLayout>

    <RelativeLayout
    android:id="@+id/badge_container"
    android:layout_width="16dp"
    android:layout_height="16dp"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="@dimen/badge_left_margin"
    android:layout_marginRight="2dp"
    android:layout_marginTop="3dp"
    android:layout_toRightOf="@+id/main_content"
    android:visibility="gone">
    <TextView
        android:id="@+id/badge_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="@android:color/white"
        android:textSize="10sp"
        android:textStyle="bold" />
</RelativeLayout>

Go to SpaceNavigationView.java class and modify this only.

public void showIconOnly(boolean value) {
    isIconOnlyMode = value;
}

and in your activity/fragment where you want to implement this simply replace this spaceNavigationView.showIconOnly(); to spaceNavigationView.showIconOnly(false);

found in this reply

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM