Android Studio BottomNavigationView图标问题

时间:2019-03-21 19:41:55

标签: android bottomnavigationview

我正在为我的应用使用BottomNavigationView。太酷了,但是当我有3个以上的项目时,这些项目会在点击时移动。

我的意思是,选中的项目获得更多边框,而其他未选中的项目则移开并粘在一起。

但是我的想法是导航菜单,例如Instagram。如果单击导航菜单中的某个项目,则所有项目将保持不变,并且不会开始向左或向右移动。

我该怎么做?

3 个答案:

答案 0 :(得分:1)

首先,应将其添加到尺寸文件中:

<dimen name="design_bottom_navigation_text_size" tools:override="true">10sp</dimen>
<dimen name="design_bottom_navigation_active_text_size" tools:override="true">10sp</dimen>

然后,申请栏中的每个项目:

@SuppressLint("PrivateResource")
fun BottomNavigationView.fixSelectedItem(position: Int) {
    val bottomMenu = this.getChildAt(0) as? BottomNavigationMenuView
    val item = bottomMenu?.getChildAt(position) as? BottomNavigationItemView
    item?.let {
        val activeLargeLabel = it.findViewById<TextView>(com.google.android.material.R.id.largeLabel)

        if (activeLargeLabel != null && activeLargeLabel is TextView) {
            activeLargeLabel.setPadding(0, 0, 0, 0)
            activeLargeLabel.ellipsize = TextUtils.TruncateAt.END
        }
    }
}
  

注意:此名称design_bottom_navigation_com.google.android.material.R在   Support Legacy个库。来自AndroidX

这是解决此缩放效果的解决方法。

答案 1 :(得分:0)

BottomNavigationView的实现具有条件:当有3个以上的项目时,请使用平移模式。 因此,要禁用此效果,您必须将此代码行添加到BottomNavigationView XML

create table test (id int not null primary key, a int)@
create table test_mqt (cnt) as (select sum(a) from test) data initially deferred refresh deferred maintained by user@

insert into test values (1, 1), (2, 1), (3, 1) with nc@

create or replace trigger test_aus 
after update on test
referencing 
new table as n
old table as o
for each statement
mode db2sql
begin atomic
  if (exists (select 1 from n,o where n.id=o.id and n.a<>o.a)) then
    refresh table test_mqt;
  end if;
end@

-- trigger IS NOT fired after the following update
update test set a=1 with nc@
-- the following select returns 0
select cnt from test_mqt@

-- trigger IS fired after the following update
update test set a=2 with nc@
-- the following select returns 6
select cnt from test_mqt@

PS:您需要支持库28.0.0及更高版本

答案 2 :(得分:-2)

您可以使用它在BottomNevigationView上同时显示3到5个项目的文本和图标,并停止移动。

 app:labelVisibilityMode="labeled"

但是您将在BottmNevigationView上遇到5个项目的长文本剪切问题。为此,我找到了一个很好的解决方案,用于停止文本以及BottomNevigationView的图标移动。您还可以停止移动文本以及BottomNevigationView上的图标。代码片段在此处给出。

1。如图所示,在BottomNevigationView中添加这行代码

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="@dimen/seventy_dp"
    android:layout_semitransparent="true"
    android:background="@color/colorBottomNev"
    android:showAsAction="always|withText"
    app:itemIconTint="@drawable/bottom_navigation_colors"
    app:itemTextColor="@drawable/bottom_navigation_colors"
    app:itemTextAppearanceActive="@style/BottomNavigationViewTextStyle"
    app:itemTextAppearanceInactive="@style/BottomNavigationViewTextStyle"
    app:menu="@menu/bottom_navigation_menu"
    app:labelVisibilityMode="labeled"/>

2。添加菜单项,如下所示:-

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_catalogue"
        android:icon="@drawable/catalogue"
        android:title="@string/catalogue"
        android:enabled="true"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_contracts"
        android:icon="@drawable/contract"
        android:title="@string/contracts"
        android:enabled="true"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_prospects"
        android:icon="@drawable/prospect"
        android:title="@string/prospects"
        android:enabled="true"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_performance"
        android:icon="@drawable/performance"
        android:title="@string/performance"
        android:enabled="true"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_advance"
        android:icon="@drawable/advance"
        android:title="@string/advance"
        android:enabled="true"
        app:showAsAction="ifRoom" />

</menu>

3。在style.xml文件中添加此样式:

 <style name="BottomNavigationViewTextStyle">
            <item name="android:fontFamily">@font/montmedium</item>
            <item name="android:textSize">10sp</item>
            <item name="android:duplicateParentState">true</item>
            <item name="android:ellipsize">end</item>
            <item name="android:maxLines">1</item>
        </style>

4)将它们添加到Dimen文件夹中

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
    <dimen name="design_bottom_navigation_text_size" tools:override="true">10sp</dimen>
    <dimen name="design_bottom_navigation_active_text_size" tools:override="true">10sp</dimen>
</resources>

我从这些linklink获得了帮助。您也可以通过研究这些链接获得帮助。这对我有很大帮助。希望这对您也有帮助。谢谢。...

相关问题