Заполнение ScrollView игнорируется при показе клавиатуры - PullRequest
6 голосов
/ 01 ноября 2019

Допустим, у меня есть такая простая раскладка:

https://imgur.com/k6zuh3f

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="80dp"
            android:clipToPadding="false">

        <LinearLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="16dp">

            <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/ic_launcher" />

            ...


            <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Eighth" />

        </LinearLayout>

    </ScrollView>

    <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_margin="16dp"
            android:text="Button" />

</RelativeLayout>

Обратите внимание, что paddingBottom на ScrollView с clipToPadding=false. Это необходимо, чтобы Button выглядел как плавающая кнопка над прокруткой. Заполнение ScrollView используется, чтобы освободить место под содержимым, чтобы сделать доступным последний дочерний элемент.

Если последним дочерним элементом является EditText, я ожидаю, что ScrollView прокрутит, сделав EditText видимым через программную клавиатуру. Но это заканчивается на этом https://imgur.com/GwOtLIq

Вид ожидаемого поведения может быть достигнут с использованием layout_marginBottom вместо paddingBottom, но в этом случае, очевидно, я не вижу своего контента за Button. Снимок экрана https://imgur.com/oSC24qV

Есть ли способ заставить ScrollView уважать его отступы с точки зрения избегания клавиатуры?

ОБНОВЛЕНИЕ: полный XML-код здесь https://pastebin.com/P8n0aZ2i

Ответы [ 4 ]

5 голосов
/ 08 ноября 2019

вам просто нужно определить тип настройки программного ввода в манифесте для вашей деятельности. Вы можете добавить следующую строку в свой манифест.

 <activity android:name=".Main2Activity" android:theme="@style/BaseTheme_FullScreen2"
              android:windowSoftInputMode="adjustResize|adjustPan">
    </activity>
0 голосов
/ 15 ноября 2019

Я не очень разбираюсь в Android, но как Java-программист я хотел бы предложить вам изменить scrollView размер в Класс активности . Пожалуйста, поместите Ваш scrollView в какой-либо другой CordinatorLayout и укажите код ниже в init () или onCreate () метод

 protected void onCreate(Bundle savedInstanceState) {
   
    mySwipeRefreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                  scrollView.setMinHeight(layoutComposite.computeSize(layoutComposite.getSize().x+100 , SWT.DEFAULT).y+100);
				
                }
            }
    );
}

Вызовите указанный выше код в обновлении активности / onload / onCreate

Intent intent=new Intent(Current_Activity.this,Current_Activity.class);
    startActivity(intent);
    finish();

см. Эту ссылку Реализация прокрутки

0 голосов
/ 11 ноября 2019

Используйте CoordinatorLayout вместо RelativeLayout, как показано ниже:

<?xml version="1.0" encoding="utf-8"?>
<CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<ScrollView
        android:id="@+id/scroll_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

        ...


        <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Eighth" />

    </LinearLayout>

</ScrollView>

<Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        app:layout_anchorGravity=”bottom|end”
        android:layout_margin="16dp"
        app:layout_anchor=”@id/scroll_container” 
        app:layout_dodgeInsetEdges="bottom"
        android:text="Button" />

</CoordinatorLayout>

Я не проверял это, но наряду с установкой android:windowSoftInputMode="adjustResize|adjustPan" для тега активности в манифесте, он должен работать так, как вы хотите.

0 голосов
/ 01 ноября 2019

Если вы хотите добиться прокрутки при открытой клавиатуре, вы должны настроить виджеты так, чтобы они не перекрывали друг друга.

Добавьте android:layout_above="@+id/button" в виджет scrollview, таким образом, он будетнастроить себя над кнопкой, когда клавиатура показывает.

Код

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="80dp"
        android:layout_above="@+id/button"
        android:clipToPadding="false">

        <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />

            ...


            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Eighth" />

        </LinearLayout>

    </ScrollView>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="16dp"
        android:text="Button" />

</RelativeLayout>

enter image description here

...