android - 底部导航 View 缩小

标签 android android-fragments searchview bottomnavigationview

当我从优惠券 fragment 更改为任何其他 fragment 时,我制作了一个带有促销、商店、奖励、优惠券和帐户选项卡的底部导航 View 的应用程序,底部导航 View 缩小,如图所示,我试过了更改布局宽度、高度并将协调器布局转换为线性布局,但没有帮助。当我仅从主页切换到任何其他选项卡时,就会出现问题。

布局文件activity_main.xml

<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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="56dp"
    android:text="@string/title_home"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="@color/white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:itemTextColor="@color/selector_bottom_navigation"
    app:itemIconTint="@color/selector_bottom_navigation"
    app:menu="@menu/navigation" />

Java 文件 MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //loading the default fragment
    loadFragment(new PromoFragment());

    //getting bottom navigation view and attaching the listener
    BottomNavigationView navigation = findViewById(R.id.navigation);
    BottomNavigationViewUtils.disableShiftMode(navigation);
    navigation.setOnNavigationItemSelectedListener(this);
}

@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
    getMenuInflater().inflate(R.menu.menu_wallet, menu);
    return super.onCreatePanelMenu(featureId, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.menu_wallet1:
            return true;
        case R.id.menu_qrcode:
            Intent intent = new Intent(this, ScannerActivity.class);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    Fragment fragment = null;

    switch (item.getItemId()) {
        case R.id.navigation_promo:
            fragment = new PromoFragment();
            break;

        case R.id.navigation_store:
            fragment = new StoreFragment();
            break;

        case R.id.navigation_reward:
            fragment = new RewardFragment();
            break;

        case R.id.navigation_coupon:
            fragment = new CouponFragment();
            break;

        case R.id.navigation_account:
            fragment = new AccountFragment();
            break;
    }

    return loadFragment(fragment);
}

private boolean loadFragment(Fragment fragment) {
    if (fragment != null) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .commit();
        return true;
    }
    return false;
}

这就是我的意思:

enter image description here

我之前将 SearchView 添加到优惠券 fragment 和 fragment 存储中

最佳答案

事实证明,如果您在 fragment 内使用协调器布局和 viewpager,您会注意到 viewpager 稍微扩展了屏幕。只需在 fragment 内禁用协调器布局的滚动功能,您就会注意到底部栏没有缩小。我知道这很奇怪,但它确实有效。

关于android - 底部导航 View 缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53552216/

相关文章:

java - 我如何在 android 中正确处理触摸事件?

android - 在 Lollipop 中清除 DNS 缓存的 Root Shell 命令?

android - 在 Android 搜索 View 中显示建议列表中的选择

android - 从 Activity 访问 ViewPager 中的 TextView

java - Fragment 类型的 getSupportFragmentManager() 方法未定义

java - 搜索 View 小部件 : move search icon to right

android - Searchview - 隐藏通过按键出现的 toast

android - 从Android ViewModel调用notifiyDataSetChanged()

android - 使用 GSON 反序列化非泛型类型的 ArrayList

android - fragment : No adapter attached skipping layout 中的 RecyclerView