web-dev-qa-db-ja.com

androidの下部ナビゲーションビューのアイコンアニメーションを削除する方法

プロジェクトにDesign Support Library 25のボトムナビゲーションビューを実装しました。ビューには5つのアイコンがあります。アイコンが選択されるたびに、アニメーションが発生します。ただし、3つ以下のアイコンでアニメーションが表示されない場合。そのアニメーションを削除して、アイコンの色を少し変更するだけです。どうすればこれを達成できますか?十分なグーグルを行いましたが、解決策が見つかりませんでした。助けてください。ありがとう。

25
Nabeel K

これから答えを得ました thread

アニメーションまたはシフトモードを削除するには

BottomNavigationViewの実装には条件があります:3つ以上のアイテムがある場合、シフトモードを使用します。

ヘルパークラスを作成する

import Android.support.design.internal.BottomNavigationItemView; 
import Android.support.design.internal.BottomNavigationMenuView; 
import Android.support.design.widget.BottomNavigationView; 
import Android.util.Log;
import Java.lang.reflect.Field;

public class BottomNavigationViewHelper { 
    public static void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try { 
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                //noinspection RestrictedApi 
                item.setShiftingMode(false);
                // set once again checked value, so view will be updated 
                //noinspection RestrictedApi 
                item.setChecked(item.getItemData().isChecked());
            } 
        } catch (NoSuchFieldException e) {
            Log.e("BNVHelper", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BNVHelper", "Unable to change value of shift mode", e);
        } 
    } 
} 

使用法

BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation_bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
86
Nabeel K

私はこれを試しましたが、うまくいきました

BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);

または、このコードmainactivity.xml

app:labelVisibilityMode="unlabeled"
9
Azad Qaderzadeh

BottomNavigationViewEx は、標準のBottomNavigationViewの優れた拡張機能です。 enableShiftingMode(false)はあなたのために仕事をします。

7
Pei

これは最もエレガントで実用的な解決策ではないかもしれませんが、BottomNavigationViewに次の行を追加してみてください。

app:labelVisibilityMode="unlabeled"

ラベルを削除し、アニメーションも無効にします。

2
bimsina

現在のバージョンを使用するとき

implementation 'com.google.Android.material:material:1.1.0-alpha06'

そして、私はlabelVisibilityModeを「labeled」に設定します

app:labelVisibilityMode="labeled"

これらの状況下で、私はそれを手に入れました

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

私もあなたを助けることができると思います。

1
coffee

これがレイアウトです

app:labelVisibilityMode="labeled"

またはコードレベルmNavigationView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);

そして、設計サポートライブラリを28.0以降に更新します。

0
Shangeeth Sivan

アニメーションを削除または移動するには、bottomNavigationViewEXを使用してbottomNavigationViewHelperクラスを作成します

package com.example.chitchat.utils;
import Android.util.Log;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;

public class BottomNavigationViewHelper {
    private static final String TAG = "bottomNavigationViewHel";

    public static void setupBottomnavigationView(BottomNavigationViewEx bottomNavigationViewEx)
    {
        Log.d(TAG, "setupBottomnavigationView: setting up bottom navigation view");

        bottomNavigationViewEx.enableAnimation(false);
        bottomNavigationViewEx.enableShiftingMode(false);
        bottomNavigationViewEx.enableItemShiftingMode(false);
        bottomNavigationViewEx.setTextVisibility(false);
    }
}
0
Gulam kadher

マテリアルデザインは使いやすくなっています。

Gradleファイルへのアプリの依存関係(最新バージョンへの更新)。

implementation 'com.google.Android.material:material:1.1.0-alpha09'

MainActivityでは、clearAnimation()関数をBottomNavigationViewクラスに呼び出すだけです。

BottomNavigationView navView = findViewById(R.id.nav_view);
navView.clearAnimation();
0
Samsruti Dash

このコードをdimens.xmlに追加するだけで、その機能は魅力的です!

<dimen name="design_bottom_navigation_active_text_size" tools:override="true">@dimen/design_bottom_navigation_text_size</dimen>
0