IT story

소프트 키보드가 표시 될 때 레이아웃을 위로 이동 하시겠습니까?

hot-time 2020. 7. 6. 07:14
반응형

소프트 키보드가 표시 될 때 레이아웃을 위로 이동 하시겠습니까?


소프트 키보드가 나타날 때 요소가 소프트 키보드에 의해 숨겨지면 하단 정렬 속성이 설정된 RelativeView에 몇 가지 요소가 있습니다.

화면 공간이 충분하면 키보드 위에 표시되거나 키보드 위의 섹션을 스크롤 가능하게하여 사용자가 여전히 요소를 볼 수 있도록 위로 이동하고 싶습니다.

이것에 접근하는 방법에 대한 아이디어가 있습니까?


예, Android 개발자 사이트 에서 프레임 워크가 표시되는 소프트 키보드를 처리하는 방법을 설명하는 이 기사를 확인하십시오 .

android:windowSoftInputMode속성을 사용하여 레이아웃별로 크기를 조정할지 또는 스크롤 할 것인지 등 활동별로 수행 할 작업을 지정할 수 있습니다.


이 코드를 onCreate()메소드 에서 사용할 수도 있습니다 .

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

활동을 위해 AndroidManifest.xml에 추가하십시오.

android:windowSoftInputMode="adjustPan|adjustResize"

다음과 같이 매니페스트 파일의 활동을 변경하십시오.

windowSoftInputMode="adjustResize"

또는

다음 onCreate()과 같이 활동 클래스에서 메소드를 변경하십시오.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

비슷한 문제 내부 스크롤보기를 구성하는 레이아웃에 직면했습니다. EditText에서 텍스트를 선택하려고 할 때마다 복사 / 잘라 내기 / 붙여 넣기 작업 표시 줄이 아래 코드와 함께 위로 이동하여 레이아웃 크기가 조정되지 않습니다.

android:windowSoftInputMode="adjustPan"

아래와 같이 수정하여

1) AndroidManifest.xml

android:windowSoftInputMode="stateVisible|adjustResize"

2) 활동 스타일의 style.xml 파일

<item name="android:windowActionBarOverlay">true</item>

효과가있었습니다. !!!!!!!!!!!!


AndroidManifest.xml에서 다음을 설정하는 것을 잊지 마십시오.

 android:windowSoftInputMode="adjustResize"

그리고 ScrollView 내부의 RelativeLayout의 경우 다음을 설정하십시오.

 android:layout_gravity="center" or android:layout_gravity="bottom" 

괜찮을거야


oncreate 메소드의 활동에 대해 아래 행 삽입

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

onCreate 메소드의 조각

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

화면을 위로 스크롤하려면 3 단계를 수행하십시오.

  1. 활동에 대한 매니페스트 파일을 작성하십시오.

android : windowSoftInputMode = "adjustResize"

  1. 활동의 oncreate () 메소드에 다음 행을 추가하십시오.

getWindow (). setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

  1. 그런 다음 XML 파일의 "Scrollview"에 전체 뷰를 배치하십시오.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="@drawable/bg"
    android:layout_height="match_parent" 
    > 
    <RelativeLayout
         android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center" >
        <TextView />
         .........
        <TextView />
        <EditText >
            <requestFocus />
        </EditText>
        <Button />
    </RelativeLayout>
 </ScrollView>

추신 부모 친척 레이아웃에 android : layout_gravity = "center"를 추가하는 것을 잊지 마십시오.


Diego Ramírez의 방법을 시도했지만 작동합니다. AndroidManifest에서 :

    <activity
        android:name=".MainActivity"
        android:windowSoftInputMode="stateHidden|adjustResize">
        ...
    </activity>

activity_main.xml에서 :

<LinearLayout ...
    android:orientation="vertical">

<Space
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1" />

<EditText
    android:id="@+id/edName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/first_name"
    android:inputType="textPersonName" />

<EditText ...>
...
</LinearLayout>

내 문제에 대한 해결책이 당신과 똑같다는 것을 발견했습니다.

우선, 2 개의 요소가있는 LinearLayout 만 있고 ImageView 및 LinearLayout에는 2 개의 EditText와 하나의 버튼이 포함되어 있으므로 키보드없이 전체 화면으로 분리해야하며 키보드가 나타나면 더 가깝게 보입니다.

그래서 0dp와 weight 1의 height 속성을 사용하여 뷰를 추가하여 뷰를 서로 다른 형식으로 분리 할 수있게했습니다. 키보드 높이가 고정 높이가 아니기 때문에 키보드 크기가 조정되면 가로 세로 크기 만 조정되고 유지됩니다.

짜잔! 키보드의 존재 여부에 따라 레이아웃 크기와 뷰 사이의 거리가 동일합니다.


다음은 전체 매니페스트 코드입니다.

<activity
        android:name=".activities.MainActivity"
        android:windowSoftInputMode="adjustResize"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

조각에 있다면 활동의 onCreate에 아래 코드를 추가해야 문제가 해결됩니다.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

매니페스트 파일에서 아래 줄을 추가하십시오. 작동합니다

<activity name="MainActivity"
    android:windowSoftInputMode="stateVisible|adjustResize">
    ...
</activity>

자, 이것은 매우 늦었지만 편집 텍스트 아래에 목록보기를 추가하면 키보드가 ListView를 이동하지 않고 해당 편집 텍스트 아래의 모든 레이아웃을 이동합니다.

<EditText
android:id="@+id/et"
android:layout_height="match_parent"
android:layout_width="match_parent"



/>

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"//can make as 1dp
    android:layout_below="@+id/et" // set to below editext
    android:id="@+id/view"
    >
**<any layout**

이것은 나를 위해 일한 유일한 솔루션입니다.


enter image description here

Here is the solution for this fix
- android:windowSoftInputMode="adjustResize" in Manifest File
- Make a class "CommentKeyBoardFix.Java" and copy and paste the below code.

public class CommentKeyBoardFix
{
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private Rect contentAreaOfWindowBounds = new Rect();

public CommentKeyBoardFix(Activity activity)
{
    FrameLayout content = activity.findViewById(android.R.id.content);
    mChildOfContent = content.getChildAt(0);
    mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(this::possiblyResizeChildOfContent);
    frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}

private void possiblyResizeChildOfContent()
{
    int usableHeightNow = computeUsableHeight();
    if (usableHeightNow != usableHeightPrevious)
    {
        int heightDifference = 0;
        if (heightDifference > (usableHeightNow /4))
        {
            // keyboard probably just became visible
            frameLayoutParams.height = usableHeightNow - heightDifference;
        }
        else
        {
            // keyboard probably just became hidden
            frameLayoutParams.height = usableHeightNow;
        }
        mChildOfContent.layout(contentAreaOfWindowBounds.left, contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom);
        mChildOfContent.requestLayout();
        usableHeightPrevious = usableHeightNow;
    }
}

private int computeUsableHeight()
{
    mChildOfContent.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds);
    return contentAreaOfWindowBounds.height();
}
}

And then call this class in your Activity or Fragment
setContentView(R.layout.your_comments_layout)
CommentKeyBoardFix(this) ---> For Kotlin
or
new CommentKeyBoardFix(this) ---> For Java

방금 최상위 레이아웃에 다음 줄을 추가하고 모든 것이 작동했습니다.

android:fitsSystemWindows="true"

참고URL : https://stackoverflow.com/questions/1964789/move-layouts-up-when-soft-keyboard-is-shown

반응형