Android 如何在EditText随文本增长时向上移动视图,以及如何在片段中仅在软键盘顶部保留视图或布局?

Android 如何在EditText随文本增长时向上移动视图,以及如何在片段中仅在软键盘顶部保留视图或布局?,android,resize,android-linearlayout,android-softkeyboard,window-soft-input-mode,Android,Resize,Android Linearlayout,Android Softkeyboard,Window Soft Input Mode,我有两个编辑文本和一个摄像头和摄像机图像视图 1) 。我希望在EditText对焦时打开软键盘时,摄像头和摄像机正好位于软键盘的顶部。 2). 此外,使用此布局,当用户在editTextUserMessage视图中输入的文本增长时,视图会朝键盘下方的底部增长。相反,我希望它长大,以便微调器和其他EditText可以向上移动。在实现这一点的同时,我希望1)实现……也就是说,我希望imageCamera和imageVideo ImageView位于键盘上方 我在activity(活动)和fragme

我有两个编辑文本和一个摄像头和摄像机图像视图

1) 。我希望在EditText对焦时打开软键盘时,摄像头和摄像机正好位于软键盘的顶部。
2). 此外,使用此布局,当用户在editTextUserMessage视图中输入的文本增长时,视图会朝键盘下方的底部增长。相反,我希望它长大,以便微调器和其他EditText可以向上移动。在实现这一点的同时,我希望1)实现……也就是说,我希望imageCamera和imageVideo ImageView位于键盘上方

我在activity(活动)和fragment(片段)中尝试了不同的softInputModes(软输入模式)。但我没有看到任何我想要的改进。
虽然看起来很有希望,但只有当焦点改变时,才能将视图保持在键盘上方,因此此代码无法始终将摄像头和摄像机图像保持在键盘上方

所有的SOUTIONS只是将整个布局向上移动,这不是我想要的。此外,这不是一项活动,而是一个片段。
你能帮我吗

下面是我的片段的布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearRootInScroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical"
        android:paddingBottom="@dimen/padding_bottom">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:orientation="vertical">

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="5dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:text="@string/prefix_type"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <Spinner
                    android:id="@+id/spinnerTypes"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:dropDownWidth="wrap_content"
                    android:padding="8dp" />

            </LinearLayout>

            <com.test.shield.views.SearchFriendCompletionView
                android:id="@+id/editTextAddFriend"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/underline_edittext"
                android:gravity="left|center"
                android:hint="@string/hint_add_friend"
                android:imeOptions="actionDone"
                android:minHeight="58dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:scrollbars="vertical"
                android:textColorHint="@color/LIGHT_BLACK" />

            <EditText
                android:id="@+id/editTextUserMessage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:background="@drawable/underline_edittext"
                android:gravity="left"
                android:hint="@string/hint_message"
                android:inputType="textCapSentences|textMultiLine"
                android:isScrollContainer="true"
                android:maxLength="140"
                android:minHeight="120dp"
                android:padding="5dp"
                android:scrollbars="vertical"
                android:textColorHint="@color/LIGHT_BLACK" />

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/linearCamAndVideo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/imageCamera"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_marginRight="5dp"
                    android:background="?attr/selectableItemBackground"
                    android:padding="10dp"
                    android:scaleType="center"
                    android:src="@drawable/ic_photo_camera_black_24dp" />

                <ImageView
                    android:id="@+id/imageVideo"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:background="?attr/selectableItemBackground"
                    android:padding="10dp"
                    android:scaleType="center"
                    android:src="@drawable/ic_videocam_black_24dp" />

            </LinearLayout>

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:gravity="start"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textTime"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black" />

            </LinearLayout>
        </LinearLayout>

        <ImageView
            android:id="@+id/imageMap"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:padding="7dp"
            android:paddingBottom="5dp"
            android:scaleType="centerCrop" />
    </LinearLayout>
</ScrollView>



好吧,我必须付出足够的努力来实现这一目标

对于2)让EditText向上生长以便光标可见的修复方法是在我的片段的onCreateView()中添加这一行,我的上面的布局是膨胀的:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u INPUT\u ADJUST\u PAN)

我尝试在清单中为活动应用相同的位,但不起作用

用于1)以保持线性布局,在可见时将摄像头和摄像机置于键盘顶部。下面是一个非常有效的解决方案。请参阅代码中的注释以了解它:

/*
- LinearCamAndVideo is the one I want to keep on top of soft Keyboard when it is visible.
Without this code, mLinearCamAndVideo will be obscured under Keyboard.

- int mInitialLayoutHeight holds no of pixels that layout is growing when it is changed while user enters text. It grows
when user enters key to next line. This globalListener will be called for every change that happens in Layout.

- mRootLayout is root layout in above layout.

- mLinearRootInScroll is root in Scrollview */
mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        if (!isRemoving()) {
            Rect r = new Rect();
            mRootLayout.getWindowVisibleDisplayFrame(r);

            int screenHeight = mRootLayout.getRootView().getHeight();

            //top of the screen starts with 0, bottom being max number of pixels which is height of screen too.
            int heightDifference = screenHeight - (r.bottom - r.top);

            mKeyboardVisible = heightDifference > (screenHeight / 3);
            if (mInitialLayoutHeight == 0) {
                mInitialLayoutHeight = mLinearCamAndVideo.getBottom();
            }

            mLayoutHeightGrown = mLinearCamAndVideo.getBottom() - mInitialLayoutHeight;

            if (mKeyboardVisible) {
                int layoutBottom = mLinearCamAndVideo.getBottom();
                int padding = heightDifference - layoutBottom + mLayoutHeightGrown;

                /*Setting padding to the parent layout in ScrollView is key as it provides
                enough extra space so that scrollTo() can move up the mLinearCamAndVideo layout. You do this only when KB is visible.*/
                mLinearRootInScroll.setPadding(0, 0, 0, padding + Math.round(Converter.convertDpToPixels(getContext(), 30)));
                mScrollView.scrollTo(0, layoutBottom);
            } else {
                   // Reset the padding when Keyboard is no more visible..
                   mLinearRootInScroll.setPadding(0, 0, 0, 0);
                mScrollView.scrollTo(0, r.top);
            }
        }

    }
};
mRootLayout.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);

嗯,我必须付出足够的努力来实现这一点

对于2)让EditText向上生长以便光标可见的修复方法是在我的片段的onCreateView()中添加这一行,我的上面的布局是膨胀的:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u INPUT\u ADJUST\u PAN)

我尝试在清单中为活动应用相同的位,但不起作用

用于1)以保持线性布局,在可见时将摄像头和摄像机置于键盘顶部。下面是一个非常有效的解决方案。请参阅代码中的注释以了解它:

/*
- LinearCamAndVideo is the one I want to keep on top of soft Keyboard when it is visible.
Without this code, mLinearCamAndVideo will be obscured under Keyboard.

- int mInitialLayoutHeight holds no of pixels that layout is growing when it is changed while user enters text. It grows
when user enters key to next line. This globalListener will be called for every change that happens in Layout.

- mRootLayout is root layout in above layout.

- mLinearRootInScroll is root in Scrollview */
mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        if (!isRemoving()) {
            Rect r = new Rect();
            mRootLayout.getWindowVisibleDisplayFrame(r);

            int screenHeight = mRootLayout.getRootView().getHeight();

            //top of the screen starts with 0, bottom being max number of pixels which is height of screen too.
            int heightDifference = screenHeight - (r.bottom - r.top);

            mKeyboardVisible = heightDifference > (screenHeight / 3);
            if (mInitialLayoutHeight == 0) {
                mInitialLayoutHeight = mLinearCamAndVideo.getBottom();
            }

            mLayoutHeightGrown = mLinearCamAndVideo.getBottom() - mInitialLayoutHeight;

            if (mKeyboardVisible) {
                int layoutBottom = mLinearCamAndVideo.getBottom();
                int padding = heightDifference - layoutBottom + mLayoutHeightGrown;

                /*Setting padding to the parent layout in ScrollView is key as it provides
                enough extra space so that scrollTo() can move up the mLinearCamAndVideo layout. You do this only when KB is visible.*/
                mLinearRootInScroll.setPadding(0, 0, 0, padding + Math.round(Converter.convertDpToPixels(getContext(), 30)));
                mScrollView.scrollTo(0, layoutBottom);
            } else {
                   // Reset the padding when Keyboard is no more visible..
                   mLinearRootInScroll.setPadding(0, 0, 0, 0);
                mScrollView.scrollTo(0, r.top);
            }
        }

    }
};
mRootLayout.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
试试这个链接试试这个链接