首页 文章

显示软键盘时向上移动布局?

提问于
浏览
111

我在RelativeView中有一些元素,对齐底部属性设置,当软键盘出现时,元素被软键盘隐藏 .

我希望它们向上移动,以便如果有足够的屏幕空间,它们会显示在键盘上方,或者使键盘上方的部分可滚动,这样用户仍然可以看到元素 .

关于如何处理这个的任何想法?

15 回答

  • 1

    我面临的类似问题我的布局包括里面的滚动视图 . 每当我尝试在EditText中选择文本时,复制/剪切/粘贴操作栏都会向下移动,因为它不会调整布局大小

    android:windowSoftInputMode="adjustPan"
    

    通过将其修改为如下

    1)AndroidManifest.xml

    android:windowSoftInputMode="stateVisible|adjustResize"
    

    2)style.xml文件,在活动样式中

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

    有效 . !!!!!!!!!!!!

  • 5

    在AndroidManifest.xml中,不要忘记设置:

    android:windowSoftInputMode="adjustResize"
    

    对于ScrollView中的RelativeLayout,设置:

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

    会没事儿的

  • 5

    for line中的oncreate methode insert中的Activity

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

    以及onCreate方法中的片段

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

    更改您的清单文件的活动,如

    windowSoftInputMode="adjustResize"
    

    OR

    在活动类中对 onCreate() 方法进行更改,例如

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

    是的,请查看this article on the Android developers' site,它描述了框架如何处理出现的软键盘 .

    android:windowSoftInputMode 属性可用于指定基于每个活动的内容:布局是否已调整大小或是否滚动等 .

  • 87

    您也可以在 onCreate() 方法中使用此代码:

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

    为您的活动添加AndroidManifest.xml:

    android:windowSoftInputMode="adjustPan|adjustResize"
    
  • 7

    我尝试了迭戈拉米雷斯的方法,它有效 . 在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>
    
  • 21

    滚动屏幕有3个步骤 .

    • 在您的活动的清单文件中写入:

    android:windowSoftInputMode =“adjustResize”

    • 在活动的oncreate()方法中添加以下行

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

    • 然后将整个视图放在"Scrollview"中的XML文件中,如:

    <?xml version =“1.0”encoding =“utf-8”?>
    <ScrollView xmlns:android =“http://schemas.android.com/apk/res/android”
    机器人:layout_width = “FILL_PARENT”
    机器人:背景= “@绘制/ BG”
    机器人:layout_height = “match_parent”

    <RelativeLayout的
    机器人:layout_width = “FILL_PARENT”
    机器人:layout_height = “match_parent”
    android:layout_gravity =“center”>
    <TextView />
    .........
    <TextView />
    <EditText>
    <requestFocus />
    </的EditText>
    <按钮/>
    </ RelativeLayout的>
    </滚动型>

    附:不要忘记在父相对布局中添加android:layout_gravity =“center” .

  • 3

    我找到了解决问题的方法,它与你的问题非常相似 .

    首先,我只有一个包含2个元素的LinearLayout,一个ImageView和一个包含2个EditText和一个Button的LinearLayout,所以我需要在没有键盘的情况下将它们分开全屏,当键盘出现时它们应该看得更近 .

    所以我添加了它们之间的视图,其高度属性为0dp,权重为1,这让我可以将视图彼此分开 . 当键盘出现时,由于它们没有固定高度,它们只是调整大小并保持方面 .

    瞧!当键盘存在或不存在时,布局调整大小并且我的视图之间的距离相同 .

  • 14

    这是完整的清单代码

    <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>
    
  • 10

    如果你是碎片,那么你必须在你的活动中将以下代码添加到你的onCreate中,它解决了我的问题

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

    好吧,这已经很晚了但是我发现如果你在编辑文本下添加一个列表视图,那么键盘将移动该edittext下的所有布局而不移动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**
    

    这是唯一对我有用的解决方案 .

  • 0

    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
    
  • 69

    在清单文件中添加以下行 . 它会工作

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

相关问题