Can not get ScrollView to scroll when soft keyboar

2019-01-23 16:36发布

问题:

I have been trying to get this scrollview to scroll, and have not had any luck in doing so. I have tried many types of fixes but cant seem to get one working. I would like to be able to scroll through my page when an edit box is selected and the keyboard takes up half the screen. This way i can navigate to the rest of the edit boxes and the accept button on the bottom of the page, with out having to manually hide the keyboard every time.

Here is my xml code:

    <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="@drawable/asuswallpaper4"
       android:orientation="vertical"
       android:weightSum="1"
      >



        <TextView
          android:id="@+id/textView1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="#AAA"
          android:text="Input Configuration"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:textColor="#000" >
         </TextView>

             <ScrollView
               android:id="@+id/input_scroll_view"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content" >

               <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >



                      <LinearLayout
                       android:id="@+id/linearLayout10"
                       android:layout_width="match_parent"
                       android:layout_height="wrap_content"
                       android:layout_marginTop="20dp"
                       android:orientation="horizontal" >

                      <TextView
                       android:id="@+id/textView13"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:text="TextView"
                       android:textAppearance="?android:attr/textAppearanceMedium"
                       android:textColor="#000"
                       android:textStyle="bold" >
                      </TextView>

            <TextView
                android:id="@+id/textView11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Enable or Disable"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFF" >
            </TextView>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout11"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp" >

            <RadioGroup
                android:id="@+id/DisableEnable"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:addStatesFromChildren="true"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/disableButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="Disable"
                    android:textColor="#FFF" >
                </RadioButton>

                <RadioButton
                    android:id="@+id/enableButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="100dp"
                    android:text="Enable"
                    android:textColor="#FFF" >
                </RadioButton>
            </RadioGroup>
        </LinearLayout>

        <!-- <LinearLayout -->
        <!-- android:id="@+id/linearLayout3" -->
        <!-- android:layout_width="match_parent" -->
        <!-- android:layout_height="wrap_content" -->
        <!-- android:layout_marginTop="20dp" -->
        <!-- android:orientation="horizontal" > -->


        <!-- <TextView -->
        <!-- android:id="@+id/textView14" -->
        <!-- android:layout_width="wrap_content" -->
        <!-- android:layout_height="wrap_content" -->
        <!-- android:text="TextView" -->
        <!-- android:textAppearance="?android:attr/textAppearanceMedium" -->
        <!-- android:textColor="#000" -->
        <!-- android:textStyle="bold" -->
        <!-- android:visibility="invisible" > -->
        <!-- </TextView> -->


        <!-- <TextView -->
        <!-- android:id="@+id/textView3" -->
        <!-- android:layout_width="wrap_content" -->
        <!-- android:layout_height="wrap_content" -->
        <!-- android:text="Positive/Negative Trigger" -->
        <!-- android:textAppearance="?android:attr/textAppearanceMedium" -->
        <!-- android:textColor="#FFF" -->
        <!-- android:visibility="invisible" > -->
        <!-- </TextView> -->
        <!-- </LinearLayout> -->


        <!-- <LinearLayout -->
        <!-- android:id="@+id/linearLayout4" -->
        <!-- android:layout_width="match_parent" -->
        <!-- android:layout_height="wrap_content" -->
        <!-- android:orientation="vertical" > -->


        <!-- <RadioGroup -->
        <!-- android:id="@+id/PosNeg" -->
        <!-- android:layout_width="wrap_content" -->
        <!-- android:layout_height="wrap_content" -->
        <!-- android:orientation="horizontal" -->
        <!-- android:visibility="invisible" android:clickable="false"> -->


        <!-- <RadioButton -->
        <!-- android:id="@+id/positive_trigger" -->
        <!-- android:layout_width="wrap_content" -->
        <!-- android:layout_height="wrap_content" -->
        <!-- android:checked="true" -->
        <!-- android:text="Positive" -->
        <!-- android:textColor="#FFF" android:clickable="false"> -->
        <!-- </RadioButton> -->


        <!-- <RadioButton -->
        <!-- android:id="@+id/negative_trigger" -->
        <!-- android:layout_width="wrap_content" -->
        <!-- android:layout_height="wrap_content" -->
        <!-- android:layout_marginLeft="100dp" -->
        <!-- android:text="Negative" -->
        <!-- android:textColor="#FFF" android:clickable="false"> -->
        <!-- </RadioButton> -->
        <!-- </RadioGroup> -->
        <!-- </LinearLayout> -->

        <LinearLayout
            android:id="@+id/linearLayout5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/textView15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000"
                android:textStyle="bold"
                android:visibility="invisible" >
            </TextView>

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Type of Trigger"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFF"
                android:visibility="invisible" >
            </TextView>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioGroup
                android:id="@+id/TypeTrigger"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:visibility="invisible" >

                <RadioButton
                    android:id="@+id/constant_trigger"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="Constant"
                    android:textColor="#FFF"
                    android:visibility="visible" >
                </RadioButton>

                <RadioButton
                    android:id="@+id/pulse_trigger"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="100dp"
                    android:text="Pulse"
                    android:textColor="#FFF" >
                </RadioButton>
            </RadioGroup>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp" >

            <TextView
                android:id="@+id/textView16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textStyle="bold"
                android:visibility="invisible" />

            <TextView
                android:id="@+id/textView17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=": Continuous or One Time (One Time Only Available for                         Pulsed Input(s))"  
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:visibility="invisible" />
            </LinearLayout>

            <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <RadioGroup
                android:id="@+id/contOneTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:visibility="invisible" >

                <RadioButton
                    android:id="@+id/continuous_trigger"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="Continuous" />

                <RadioButton
                    android:id="@+id/oneTime_trigger"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="80dp"
                    android:text="One Time" />
            </RadioGroup>
           </LinearLayout>

           <LinearLayout
            android:id="@+id/linearLayout7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:orientation="vertical"
            android:visibility="visible" >

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Timer Configuration For Selected Input"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFF"
                android:visibility="invisible" >
            </TextView>
           </LinearLayout>


           <LinearLayout
            android:id="@+id/linearLayout8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Number of Pulses"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFF"
                android:visibility="invisible" >
            </TextView>

            <EditText
                android:id="@+id/NumberOfPulses"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="0"
                android:inputType="number"
                android:numeric="integer"
                android:visibility="invisible" >

                <requestFocus>
                </requestFocus>
            </EditText>

            <TextView
                android:id="@+id/textView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.34"
                android:text=" within,"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFF"
                android:visibility="invisible" >
            </TextView>
           </LinearLayout>

           <LinearLayout
            android:id="@+id/linearLayout9"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:visibility="visible" >

            <EditText
                android:id="@+id/Minutes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.00"
                android:hint="0"
                android:inputType="number"
                android:numeric="integer"
                android:visibility="invisible" >
            </EditText>

            <TextView
                android:id="@+id/textView8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.00"
                android:text="Minutes,"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFF"
                android:visibility="invisible" >
            </TextView>

            <EditText
                android:id="@+id/Seconds"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.00"
                android:hint="0"
                android:inputType="number"
                android:numeric="integer"
                android:visibility="invisible" >
            </EditText>

            <TextView
                android:id="@+id/textView9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Seconds,"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFF"
                android:visibility="invisible" >
            </TextView>

            <EditText
                android:id="@+id/Milliseconds"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.00"
                android:hint="0"
                android:inputType="number"
                android:numeric="integer"
                android:visibility="invisible" >
            </EditText>

            <TextView
                android:id="@+id/textView10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Milliseconds."
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFF"
                android:visibility="invisible" >
            </TextView>

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.00"
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:visibility="invisible" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout13"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:weightSum="1" >

            <Button
                android:id="@+id/inputAccept"
                android:layout_width="159dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.04"
                android:text="Accept" >
            </Button>

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button"
                android:visibility="invisible" >
            </Button>

            <Button
                android:id="@+id/inputSave"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.06"
                android:text="Save"
                android:visibility="invisible" >
            </Button>

            <Button
                android:id="@+id/button5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button"
                android:visibility="invisible" >
            </Button>

            <Button
                android:id="@+id/inputLoad"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.05"
                android:text="Load"
                android:visibility="invisible" >
                </Button>
            </LinearLayout>
         </LinearLayout>
       </ScrollView>

    </LinearLayout>

(Please someone help me figure this thing out, its driving me crazy!!). I would also like to mention that I have tried most of the fixes with people who had similar issues such as filling the view port, and enabling the vertical scroll bar, non of these helped me with my troubles. I also tried adding them to the parent layout and did not seem to work either.

回答1:

Add android:windowSoftInputMode="adjustResize" to the <activity> tag in your AndroidManifest.xml. This will cause the screen to be resized to the space left over after the software keyboard is shown. As a result, you will be able to scroll, since the screen will not be covered by the keyboard in any way.

EDIT:

I have written a minimal example and tested it. Unless there is a huge misunderstanding, try this code and then figure out why yours doesn't work:

xml layout:

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

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >

        <TextView
            android:layout_height="2000dp"
            android:layout_width="wrap_content"
            android:gravity="top"
            android:text="Scroll Down!"/>

        <EditText
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:hint="Enter Text"
            />
    </LinearLayout>
</ScrollView>

manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="15"/>
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="MyActivity"
                  android:label="@string/app_name"
                android:windowSoftInputMode="adjustResize"
                >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>


回答2:

Hi I encountered this problem as well. The issue I had was I added adjustResize as shown below in my Manifest

and it still wouldn't scroll. But in my xml file in the beginning of my ScrollView

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/top"
    android:scrollbars="none" >

I deleted android:scrollbars="none" and that solved my problem. Just for anyone whose adjustment on the manifest did not work.