【问题标题】:Android - LinearLayout not scrolling inside ScrollVIewAndroid - LinearLayout 不在 ScrollVIew 内滚动
【发布时间】:2018-06-12 21:51:07
【问题描述】:

我的问题是 LinearLayout 在 ScrollView 内无法滚动,而是似乎超出了屏幕框架(仔细查看下面链接的屏幕截图的底部)。

我的 XML 布局中有以下结构:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </android.support.design.widget.TextInputLayout>

            <!-- More TextInputLayouts -->

        </LinearLayout>
    </ScrollView>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src = "@drawable/ic_done"
        android:tint = "@color/white"/>
</android.support.design.widget.CoordinatorLayout>

没有找到能够解决问题的解决方案:我尝试了 fillViewPort="true" 但它没有为我解决问题,我还尝试评论 CoordinatorLayout - 结果相同,layout_heights 似乎也设置正确。

此外,我尝试以编程方式调整 LinearLayout 的高度,但也无济于事。

我被这个问题困扰了一段时间,非常感谢这方面的任何帮助:)

Screenshot

【问题讨论】:

  • 尝试在您的 ScrollView 上使用 wrap_content,甚至可能已经出现警告。
  • 您是否尝试在 xml 布局的 ScrollView 内添加 android:fillViewport="true"
  • @Nicolas 没用
  • @bko ScrollView 发生了变化,因此它不再从屏幕底部脱离,但内部的 LinearLayout 仍然不可滚动
  • @IliaGromov 尝试设置线性布局的高度以匹配父级...

标签: java android xml android-linearlayout android-scrollview


【解决方案1】:

它适用于我下面的 XML。我没有做太多改变,只是在 LinearLayout 中添加了一个方向以及一些边距和糟糕的背景颜色,这样您就可以在滚动时看到哪个视图在移动,哪个视图是固定的。

当您上下拖动时,蓝绿色框(LinearLayout)会在红色框(ScrollView)内按预期上下移动。如果您的 LinearLayout 比屏幕高,它会离开底部,这就是 ScrollView 的点。

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:background="#ff0000"
    android:scrollbars="vertical">
    <LinearLayout
        android:orientation="vertical"
        android:layout_margin="16dp"
        android:background="#008080"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    </LinearLayout>
</ScrollView>

</android.support.design.widget.CoordinatorLayout>

【讨论】:

    【解决方案2】:

    您在 xml 代码中放置了线性布局方向 喜欢..

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

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

    【讨论】:

      【解决方案3】:

      试试这个:-

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      
       xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/whiteColor"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">
      <!-- vertical ScrollView to make all the items or views scrollable -->
      <ScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:scrollbars="none">
       <!-- LinearLayout Inside ScrollView -->
       <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">
      <!-- create a Linear Layout with horizontal orientation and weightSum property -->
      <LinearLayout
        android:id="@+id/firstLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal"
        android:weightSum="2">
       <EditText
       android:id="@+id/lastName"
       android:layout_width="0dp"
       android:layout_height="50dp"
       android:layout_marginLeft="10dp"
       android:layout_weight="1.4"
       android:background="@color/editTextBack"
       android:hint="Last Name"
       android:imeOptions="actionNext"
       android:paddingLeft="10dp"
       android:singleLine="true"
       android:textColor="@color/blackColor" />
       </LinearLayout>
       <!-- create a Linear Layout with horizontal orientation and weightSum property -->
      
       <LinearLayout
        android:id="@+id/thirdLayout"
         android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="20dp"
         android:orientation="horizontal"
          android:weightSum="2">
      
          <!-- place one TextView and one EditText inside layout using weight property -->
      
              <TextView
                  android:layout_width="0dp"
                  android:layout_height="50dp"
                  android:layout_marginRight="10dp"
                  android:layout_weight="0.6"
                  android:gravity="center_vertical"
                  android:paddingLeft="10dp"
                  android:text="Address"
                  android:textColor="@color/blackColor" />
      
      
       </LinearLayout>
       </ScrollView>
      
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        • 1970-01-01
        • 2013-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多