ScrollView cuts off the top and leaves space at the bottom

后端 未结 8 860
谎友^
谎友^ 2021-02-03 19:41

When I launch the emulator and enter the screen which uses this code it shows most of the text information but cuts off the top of the screen (cant scroll up) but leaves a bit o

相关标签:
8条回答
  • 2021-02-03 19:45

    You must add marginBottom in LinearLayout

    <ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:visibility="visible"
    android:fillViewport="true"
    android:id="@+id/backgroundImage" >
    
     <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:padding="10dip"
    android:layout_marginBottom="96dp" >    
    
     <ImageView
        android:id="@+id/earthSymbolImageView"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:src="@drawable/earthsymbol" />
    
     <TextView
        android:id="@+id/earth_content1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/earth_title"
        android:gravity="center" 
        android:textColor="#FFFFFF"
        android:textSize="20sp" />
    
    <TextView
        android:id="@+id/earth_content2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/earth_text" 
        android:textColor="#FFFFFF" />
    
    <Button
        android:id="@+id/backButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/back" />
    
     </LinearLayout>
    </ScrollView>
    
    0 讨论(0)
  • 2021-02-03 19:46

    In my case the problem was requestFocus of EditText.

    In your case their is no EditText so the problem is something other. For those who are having problem of scrollview cuts top edges, Try to remove all requestFocus from all EditText.

    0 讨论(0)
  • 2021-02-03 19:46

    I had a the same problem with aHorizontalScrollViewnested inside aScrollView.I had theHorizontalScrollViewgravity set tocenterwhich was causing the problem. Just removing it fixed the issue.

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
    <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center">
    
        <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
    
            <TextView
                    android:layout_width="1000dp"
                    android:layout_height="1000dp"
                    android:gravity="center"
                    android:layout_gravity="center"
                    android:text="New Text"/>
        </LinearLayout>
    </HorizontalScrollView>
    
    0 讨论(0)
  • 2021-02-03 19:51

    The best answer I have found and tested that fixes this problem is the one that @hadi gave in this question: no gravity for scrollview. how to make content inside scrollview as center

    Resuming the answer:

    • Put your ScrollView inside a RelativeLayout.
    • In the ScrollView, set the following:
      • android:layout_height="wrap_content"
      • android:layout_centerVertical="true"
    • Put your vertical LinearLayout inside the ScrollView.

    The code would be:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                ...
    
            </LinearLayout>
        </ScrollView>
    </RelativeLayout>
    
    0 讨论(0)
  • 2021-02-03 19:58

    This is being caused because of the layout_gravity in your LinearLayout. Since your LinearLayout is inside a ScrollView you are probably just trying to center horizontally (centering vertically inside a ScrollView doesn't make since). Specifying your LinearLayout to center horizontally like this should do the trick:

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center_horizontal"
        android:padding="10dip" >
    
    0 讨论(0)
  • 2021-02-03 20:00

    Its quite simple - you just need to move layout_gravity from LinearLayout into your parent container ScrollView:

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:visibility="visible"
        android:fillViewport="true"
        android:id="@+id/backgroundImage" 
        android:layout_gravity="center" >
    
     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip" >
    
    0 讨论(0)
提交回复
热议问题