【问题标题】:ScrollView not scrolling as parent and RelativeLayout as childScrollView 不作为父级滚动,RelativeLayout 作为子级滚动
【发布时间】:2021-02-07 17:47:54
【问题描述】:

我知道这个问题已经被问过好几次了,但没有一个答案对我有用,所以我再次重新发布它。 我使用滚动视图作为父布局,使用相对布局作为子布局。 这是我的代码:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:background="@drawable/grdnt">
    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"    >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:paddingLeft="60dp"
            android:paddingRight="60dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                >
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:padding="16dp">
                    <ImageView
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:src="@drawable/ic_ip"
                        android:layout_gravity="center"/>
                    <EditText
                        android:id="@+id/ip"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Server Adress"
                        android:textColorHint="#d9e5f3"
                        android:layout_marginLeft="15dp"
                        android:background="#00000000"
                        android:letterSpacing="0.1"
                        android:textSize="16dp"
                        android:inputType="text"
                        android:textColor="#fff"
                        android:fontFamily="@font/lato"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#4a5a71"></LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:padding="16dp">
    
                    <ImageView
                        android:layout_width="18dp"
                        android:layout_height="20dp"
                        android:layout_gravity="center"
                        android:src="@drawable/ic_g" />
                    <EditText
                        android:id="@+id/port"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Port"
                        android:textColorHint="#d9e5f3"
                        android:layout_marginLeft="15dp"
                        android:background="#00000000"
                        android:inputType="text"
                        android:letterSpacing="0.1"
                        android:textSize="16dp"
                        android:textColor="#fff"
                        />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#4a5a71"></LinearLayout>
    
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="60dp"
            android:paddingRight="60dp"
            android:orientation="vertical"
            android:gravity="bottom"
            android:layout_marginBottom="20dp">
            <TextView
                android:id="@+id/sub"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#009a9a"
                android:text="Submit"
                android:textColor="#d9e5f3"
                android:textStyle="bold"
                android:gravity="center"
                android:layout_gravity="bottom"
                android:padding="16dp"
                android:layout_marginBottom="50dp"
                android:letterSpacing="0.2"/>
    
    
        </LinearLayout>
        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_marginBottom="400dp">
        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:src="@drawable/ic_settings"
            />
    </LinearLayout>
</RelativeLayout>
</ScrollView>

我尝试将它放在 LinearLayout 中但仍然没有,我还尝试将 RelativeLayout 更改为 LinearLayout 并且什么也没有。

有什么建议吗?

【问题讨论】:

  • 你试过这个吗:&lt;LinearLayout .. &lt;ScrollView .. &lt;/ScrollView&gt; .. &lt;/LinearLayout&gt;
  • 您使用了太多布局尝试使用ConstraintLayout 而不是在ScrollView 中使用wrap_content 作为ConstraintLayout's height

标签: android android-layout android-xml


【解决方案1】:

用这个

   <?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:background="@drawable/user">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:layout_marginBottom="20dp">
                <ImageView
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:src="@drawable/images"
                    />
            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:paddingLeft="60dp"
                android:paddingRight="60dp"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="20dp">

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

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:padding="16dp">
                        <ImageView
                            android:layout_width="20dp"
                            android:layout_height="20dp"
                            android:src="@drawable/images"
                            android:layout_gravity="center"/>
                        <EditText
                            android:id="@+id/ip"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="Server Adress"
                            android:textColorHint="#d9e5f3"
                            android:layout_marginLeft="15dp"
                            android:background="#00000000"
                            android:letterSpacing="0.1"
                            android:textSize="16dp"
                            android:inputType="text"
                            android:textColor="#fff"/>
                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="#4a5a71"></LinearLayout>
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:padding="16dp">

                        <ImageView
                            android:layout_width="18dp"
                            android:layout_height="20dp"
                            android:layout_gravity="center"
                            android:src="@drawable/images" />
                        <EditText
                            android:id="@+id/port"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="Port"
                            android:textColorHint="#d9e5f3"
                            android:layout_marginLeft="15dp"
                            android:background="#00000000"
                            android:inputType="text"
                            android:letterSpacing="0.1"
                            android:textSize="16dp"
                            android:textColor="#fff"
                            />
                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="#4a5a71"></LinearLayout>

                </LinearLayout>
            </LinearLayout>
            
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="60dp"
                android:paddingRight="60dp"
                android:orientation="vertical"
                android:gravity="bottom"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="400dp">

                <TextView
                    android:id="@+id/sub"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#009a9a"
                    android:text="Submit"
                    android:textColor="#d9e5f3"
                    android:textStyle="bold"
                    android:gravity="center"
                    android:layout_gravity="bottom"
                    android:padding="16dp"
                    android:layout_marginBottom="50dp"
                    android:letterSpacing="0.2"/>


            </LinearLayout>


        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</LinearLayout>

【讨论】:

    【解决方案2】:

    好的,我想出了问题所在,我希望有一天这对某人有所帮助。 问题是我的 style.xml 文件我使用的是“windowFullscreen”项,它阻止了应用滚动。

    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        ...
        <item name="android:windowFullscreen">true</item>
    </style>
    

    所以我所要做的就是删除那个全屏属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多