键盘向上移动了底部导航栏(布局)

时间:2019-06-13 08:33:39

标签: java android

我正在使用LinearLayout使用自定义底部导航栏。因此,每当关注编辑文本字段时,底部视图就会随键盘一起出现。

我尝试过更改清单中的android:windowSoftInputMode="adjustPan"android:windowSoftInputMode="adjustResize"。它可以工作,但使用时无法滚动。

聚焦键盘: When focusing keyboard

未将键盘对准焦点: Bottom navigation view when not focusing keyboard

 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="com.sns.lovelike.activity.user.UserActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_50sdp"
        android:layout_alignParentBottom="true"
        android:layout_gravity="end|bottom"
        android:background="@color/colorPrimaryDark"
        android:orientation="horizontal"
        android:padding="@dimen/_5sdp"
        android:weightSum="5">


        <LinearLayout
            android:id="@+id/home"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/home_iv"
                android:layout_width="@dimen/_18sdp"
                android:layout_height="@dimen/_18sdp"
                android:layout_gravity="center"
                android:src="@drawable/home_icon_normal" />

            <TextView
                android:id="@+id/home_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/user_home"
                android:textColor="@color/colorLightGray"
                android:textSize="@dimen/_10sdp" />

            <View
                android:id="@+id/hv"
                android:layout_width="@dimen/_32sdp"
                android:layout_height="@dimen/_1sdp"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/_1sdp"
                android:background="@color/colorWhite"
                android:backgroundTint="@color/colorRed"
                android:visibility="invisible"
                tools:visibility="visible" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/love"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/love_iv"
                android:layout_width="@dimen/_18sdp"
                android:layout_height="@dimen/_18sdp"
                android:layout_gravity="center"
                android:src="@drawable/heart_icon_normal" />

            <TextView
                android:id="@+id/love_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/user_in_love"
                android:textColor="@color/colorLightGray"
                android:textSize="@dimen/_10sdp" />

            <View
                android:id="@+id/iv"
                android:layout_width="@dimen/_42sdp"
                android:layout_height="@dimen/_1sdp"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/_1sdp"
                android:background="@color/colorWhite"
                android:backgroundTint="@color/colorRed"
                android:visibility="invisible"
                tools:visibility="visible" />

        </LinearLayout>

        <View
            android:layout_width="0dp"
            android:layout_height="@dimen/_60sdp"
            android:layout_marginBottom="@dimen/_20sdp"
            android:layout_weight="1"
            android:elevation="15dp"
            android:src="@drawable/search_icon_bottom" />

        <LinearLayout
            android:id="@+id/love_coin"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/coin_iv"
                android:layout_width="@dimen/_18sdp"
                android:layout_height="@dimen/_18sdp"
                android:layout_gravity="center"
                android:src="@drawable/ilove_coin_normal" />

            <TextView
                android:id="@+id/coin_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/user_lov_coins"
                android:textColor="@color/colorLightGray"
                android:textSize="@dimen/_10sdp" />

            <View
                android:id="@+id/cv"
                android:layout_width="@dimen/_50sdp"
                android:layout_height="@dimen/_1sdp"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/_1sdp"
                android:background="@color/colorWhite"
                android:backgroundTint="@color/colorRed"
                android:visibility="invisible"
                tools:visibility="visible" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/pager"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/pager_iv"
                android:layout_width="@dimen/_18sdp"
                android:layout_height="@dimen/_18sdp"
                android:layout_gravity="center"
                android:src="@drawable/pager_normal" />

            <TextView
                android:id="@+id/pager_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/user_pay"
                android:textColor="@color/colorLightGray"
                android:textSize="@dimen/_10sdp" />

            <View
                android:id="@+id/pv"
                android:layout_width="@dimen/_40sdp"
                android:layout_height="@dimen/_1sdp"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/_1sdp"
                android:background="@color/colorWhite"
                android:backgroundTint="@color/colorRed"
                android:visibility="invisible"
                tools:visibility="visible" />

        </LinearLayout>


    </LinearLayout>

    <ImageView
        android:id="@+id/search_iv"
        android:layout_width="@dimen/_50sdp"
        android:layout_height="@dimen/_50sdp"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:layout_marginBottom="@dimen/_20sdp"
        android:src="@drawable/search_icon_bottom"
        app:layout_anchor="@+id/linearLayout2"
        app:layout_anchorGravity="top|center" />

</android.support.design.widget.CoordinatorLayout>

下面是Java代码,这是底部导航的click动作。与其他三个项目相似。

private void homeClickAction() {
    //if (!homeIsActive) {
    fragment = new HomeFragment();
    homeIV.setColorFilter(getResources().getColor(R.color.colorWhite));
    homeTV.setTextColor(getResources().getColor(R.color.colorWhite));
    loveIV.setColorFilter(null);
    loveTV.setTextColor(getResources().getColor(R.color.colorLightGray));
    coinsIV.setColorFilter(null);
    coinsTV.setTextColor(getResources().getColor(R.color.colorLightGray));
    pagerIV.setColorFilter(null);
    pagerTV.setTextColor(getResources().getColor(R.color.colorLightGray));
    v1.setVisibility(View.VISIBLE);
    v2.setVisibility(View.INVISIBLE);
    v3.setVisibility(View.INVISIBLE);
    v4.setVisibility(View.INVISIBLE);
    homeClicked = true;

}

1 个答案:

答案 0 :(得分:1)

如果您希望该键盘不要向上推底栏,而应保持在键盘后面,则只需在活动中添加此标签即可

android:windowSoftInputMode="adjustNothing"

否则,您可以通过某种方式做到这一点,添加键盘可见/隐藏事件,并相应地显示/隐藏底部栏。喜欢

mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        if (mainLayout != null) {
            int heightDiff = mainLayout.getRootView().getHeight() - mainLayout.getHeight();
            if (heightDiff > 200) { 
               //keyboard is open, hide layout
            } else {
               //keyboard is hidden, show layout
            }
        }
    }
});