【问题标题】:ScrollView not working in Android滚动视图在 Android 中不起作用
【发布时间】:2012-11-14 13:00:19
【问题描述】:

我的布局中的 ScrollView 不起作用。这个问题可能很愚蠢。我已经浪费了一个小时。

这是我的 XML 布局:

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="1100dp"
    android:layout_height="1500dp"
    android:fillViewport="true"
    android:background="#f7f7f7"
    android:layout_weight="1"
    android:orientation="vertical"
    android:padding="25dp" >

<LinearLayout
    android:id="@+id/container"
    android:layout_width="1100dp"
    android:layout_height="fill_parent" 
    android:orientation="vertical">

    <TextView
        android:id="@+id/account_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:text="ACCOUNT"
        android:textColor="#f44b3b"
        android:textSize="18dp" />

    <LinearLayout
        android:layout_width="900dp"
        android:layout_height="2dp"
        android:layout_marginBottom="10dp"
        android:background="#f44b3b" >
    </LinearLayout>

    <RelativeLayout
        android:layout_width="1100dp"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:text="Email"
            android:textColor="#333333"
            android:textSize="22dp" />

        <EditText
            android:id="@+id/email"
            android:layout_width="500dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="300dp"
            android:layout_toRightOf="@+id/textView1"
            android:ems="10"
            android:inputType="textEmailAddress" >
        </EditText>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="900dp"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:background="#c6c6c6" >
    </LinearLayout>

    <RelativeLayout
        android:layout_width="1100dp"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/change_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:text="Change Password"
            android:textColor="#333333"
            android:textSize="22dp" />

        <EditText
            android:id="@+id/change_password_et"
            android:layout_width="500dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="170dp"
            android:layout_toRightOf="@+id/change_password"
            android:ems="10"
            android:inputType="text" >
        </EditText>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="900dp"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:background="#c6c6c6" >
    </LinearLayout>

    <RelativeLayout
        android:layout_width="1100dp"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/facebook"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:text="Facebook"
            android:textColor="#333333"
            android:textSize="22dp" />

        <EditText
            android:id="@+id/facebook_et"
            android:layout_width="500dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="255dp"
            android:layout_toRightOf="@+id/facebook"
            android:ems="10"
            android:inputType="text" >
        </EditText>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="900dp"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:background="#c6c6c6" >
    </LinearLayout>

    <RelativeLayout
        android:layout_width="1100dp"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/twitter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:text="Twitter"
            android:textColor="#333333"
            android:textSize="22dp" />

        <EditText
            android:id="@+id/twitter_et"
            android:layout_width="500dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="280dp"
            android:layout_toRightOf="@+id/twitter"
            android:ems="10"
            android:inputType="text" >
        </EditText>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="900dp"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:background="#c6c6c6" >
    </LinearLayout>

    <TextView
        android:id="@+id/profile_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:text="Heading"
        android:textColor="#f44b3b"
        android:textSize="18dp" />

    <LinearLayout
        android:layout_width="900dp"
        android:layout_height="2dp"
        android:layout_marginBottom="10dp"
        android:background="#f44b3b" >
    </LinearLayout>

    <RelativeLayout
        android:layout_width="1100dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp" >

        <TextView
            android:id="@+id/Gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:text="Gender"
            android:textColor="#333333"
            android:textSize="22dp" />

        <Spinner
            android:id="@+id/gender_spinner"
            android:layout_width="500dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="280dp"
            android:layout_toRightOf="@+id/Gender" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="900dp"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:background="#c6c6c6" >
    </LinearLayout>

    <RelativeLayout
        android:layout_width="1100dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp" >

        <TextView
            android:id="@+id/Birthday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="170dp"
            android:text="Birthday"
            android:textColor="#333333"
            android:textSize="22dp" />

        <Button
            android:id="@+id/setDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/Birthday"
            android:onClick="showDatePickerDialog"
            android:text="Pick date" />

        <EditText
            android:id="@+id/birthdayet"
            android:layout_width="500dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:layout_toRightOf="@+id/Birthday"
            android:ems="10"
            android:inputType="text"
            android:text="Birthday"
            android:textColor="#f44b3b"
            android:textSize="18dp" >
        </EditText>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="900dp"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:background="#c6c6c6" >
    </LinearLayout>

    <RelativeLayout
        android:layout_width="1100dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp" >

        <TextView
            android:id="@+id/zipcode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:text="Zip Code"
            android:textColor="#333333"
            android:textSize="22dp" />

        <EditText
            android:id="@+id/zip"
            android:layout_width="500dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="270dp"
            android:layout_toRightOf="@+id/zipcode"
            android:ems="10"
            android:inputType="number" >
        </EditText>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="900dp"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:background="#c6c6c6" >
    </LinearLayout>

    <TextView
        android:id="@+id/heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:text="COOL"
        android:textColor="#f44b3b"
        android:textSize="18dp" />

    <LinearLayout
        android:layout_width="900dp"
        android:layout_height="2dp"
        android:layout_marginBottom="8dp"
        android:background="#f44b3b" >
    </LinearLayout>      

</LinearLayout>

</ScrollView>

【问题讨论】:

  • 不工作是什么意思?问题是什么?你有没有在xml中添加?
  • 使用 android:layout_width="fill_parent " android:layout_height="fill_parent" 并移除布局权重
  • 你要水平滚动还是垂直滚动?
  • @TechEnd 效果很好。伟大的。谢谢!我怎么能接受这个作为答案? :)
  • 您不能接受评论作为答案。最好你发布你的答案并接受它..

标签: android android-layout layout scrollview


【解决方案1】:

删除

android:layout_weight="1"

android:orientation="vertical" 

从滚动视图。还有fill_parent布局高度..

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="1100dp"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:background="#f7f7f7"
    android:padding="25dp" >

【讨论】:

    【解决方案2】:

    在 ScrollView 属性中替换了这个

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    

    也删除了

    android:layout_weight ="1"
    

    这很好用。感谢@TechEnd

    【讨论】:

      【解决方案3】:

      您只需更改 Scrollview 属性,如下所示:

      <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:fillViewport="true"
      android:background="#f7f7f7"
      android:layout_weight="1"
      android:orientation="vertical"
      android:padding="25dp" >
      

      【讨论】:

        【解决方案4】:

        从此活动的清单中删除:"android:windowSoftInputMode="adjustPan"

        它一定会起作用的。

        【讨论】:

        【解决方案5】:

        你需要关闭你的

        &lt; ScrollView&gt; &lt;/ScrollView&gt;

        我没看到或者你只是忘记发布了???

        【讨论】:

          【解决方案6】:
                    <ScrollView
                          android:layout_width="match_parent"
                          android:layout_height="40dp"
                          android:fadeScrollbars="false"
                          android:scrollbarStyle="insideInset"
                          android:scrollbarThumbVertical="@drawable/scrollview_thumb"
                          android:scrollbarTrackVertical="@drawable/custom_scroll_style">
                      <EditText
                          android:id="@+id/input_customer_email"
                          android:layout_width="match_parent"
                          android:layout_height="40dp"
                          android:clickable="false"
                          android:longClickable="false"
                          android:focusable="false"
                          android:inputType="textMultiLine"
                          android:hint="Customer's Email Address" />
                      </ScrollView>
          

          【讨论】:

          • 欢迎来到 SO!当您对代码回复问题时,请尝试稍微解释一下。在这种情况下,还有 5 个答案,所以请公开你的优点。
          【解决方案7】:

          在我的例子中,ListView 将持有一个适配器,用于填充垂直堆栈的按钮。我已经手动计算并分配了 ListView 高度(以所有按钮都展开的方式)。我遇到的滚动问题是,在 ScrollView 内使用 ListView (滚动发生的地方,如果我在 ListView 的边界之外单击并尝试滚动。而滚动不会发生,如果我尝试通过在 ListView 的边界内单击来滚动) .我浏览了多个 stackoverflow 答案/android 文档,还尝试了一些自定义 ListView/LinearLayout(带适配器),但最终没有任何效果。然后,我理解了逻辑并让 ListView 具有 NestedScrolling 实现,它就像魅力一样工作。

          我刚刚对我的代码进行了这个更改

          myListView.setNestedScrollingEnabled(true);
          

          【讨论】:

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