简体   繁体   English

滚动视图未显示 android 中的所有视图

[英]Scroll View not showing all the views in android

I am unable to see the top most views under ScrollView.我无法在 ScrollView 下看到最多的视图。 I had placed the scrollview under Relative layout.我已将滚动视图放在相对布局下。 ScrollView has a child layout ie Linear layout which has some series of buttons. ScrollView 有一个子布局,即具有一系列按钮的线性布局。 Problem is that i am unable to see the top most views which are present in the ScrollView.问题是我无法看到 ScrollView 中存在的最顶层视图。

Below is the layout xml code.下面是布局 xml 代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#217d27"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:onClick="backPressed"
            android:text="Back"
            android:textColor="#000" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:text="Heading Text"
            android:textColor="#fff"
            android:textSize="20sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:onClick="homeButtonClicked"
            android:text="Home"
            android:textColor="#000" />
    </RelativeLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fadingEdge="none"
        android:fillViewport="true" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#fff"
            android:orientation="vertical" >

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Button 1"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 2"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 3"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 4"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 5"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 6"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 7"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 8"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 9"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 10"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 11"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 12"
                android:textColor="@color/black" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

Please see the screen shot for reference.请参阅屏幕截图以供参考。

截屏

just remove this line in linear layout which is direct child of scroll view android:layout_gravity="center" try this code which is updated只需删除线性布局中的这一行,它是滚动视图 android:layout_gravity="center" 的直接子级,试试这个更新的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#217d27"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:onClick="backPressed"
            android:text="Back"
            android:textColor="#000" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:text="Heading Text"
            android:textColor="#fff"
            android:textSize="20sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:onClick="homeButtonClicked"
            android:text="Home"
            android:textColor="#000" />
    </RelativeLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fadingEdge="none"
        android:fillViewport="true" >

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

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Button 1"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 2"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 3"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 4"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 5"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 6"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 7"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 8"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 9"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 10"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 11"
                android:textColor="@color/black" />

            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:text="Button 12"
                android:textColor="@color/black" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

Just change the LinearLayout inside the ScrollView只需更改ScrollView内的LinearLayout

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center"
        android:background="#fff"
        android:orientation="vertical" >

Give the android:layout_gravity="top|center" instead of android:layout_gravity="center"android:layout_gravity="top|center"而不是android:layout_gravity="center"

Hope it helps.希望它有帮助。

这个值帮助我解决了一个类似的问题:在 ScrollView 上设置 android:fillViewport="true" 并在 ScrollView 子上使用 android: gravity而不是 android: layout_gravity ,在我的情况下是清除所有权重的 LinearLayout。

Try changing the parent layout to Linear.尝试将父布局更改为线性。 It might work.它可能会起作用。 Don't know how and why, but for me it worked in a similar situation.不知道如何以及为什么,但对我来说它在类似的情况下工作。 Give it a try.试一试。

Give a top padding of 50dp提供 50dp 的顶部填充

 <LinearLayout
            android:id="@+id/linearLayout"
            android:paddingTop="50dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

在 LinearLayout 中使用 android:layout_gravity="center"

it's work 100% 

 1. LinearLayout ->
    2. ScrollView-> 

<androidx.appcompat.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".sales.CustomerDetailListActivity">
       <ScrollView
            android:id="@+id/layoutCard"
            app:layout_constraintTop_toBottomOf="@+id/subtitle_of_page"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="4dp"
            android:layout_marginLeft="4dp"
            android:fillViewport="false"
            android:fadingEdge="none">
</ScrollView>
</androidx.appcompat.widget.LinearLayoutCompat>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM