【问题标题】:Why all the contents in a scrollview are not being shown?为什么没有显示滚动视图中的所有内容?
【发布时间】:2018-08-27 17:30:23
【问题描述】:

下面是我的 XML 文件。我已将所有内容放在线性布局内的 ScrollView 中,如您所见,包含所有视图的另一个布局包含在 ScrollView 内,但我的最后一个按钮小部件不可见在运行。可能的原因和解决方案是什么?

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn1"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:text="Overview"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn2"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:text="Program Structure"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn3"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:text="Tokens , Semicolons and Comments"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:id="@+id/btn4"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:text="Data Types"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn5"
            android:text="Variables"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn6"
            android:text="Constants and Literals"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn7"
            android:text="Operators"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn8"
            android:text="Decision Making"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn9"
            android:text="Loops"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn10"
            android:text="Functions"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn11"
            android:text="Scope Rules"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn12"
            android:text="Arrays"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:id="@+id/btn13"
            android:textColor="#fff"
            android:text="Pointers"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn14"
            android:text="Strings"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn15"
            android:text="Structures"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn16"
            android:text="Typedef"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn17"
            android:text="Input and Output"/>
        <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@color/colorPrimaryDark"
        android:textColor="#fff"
        android:id="@+id/btn18"
        android:text="File I/O"/>

        <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@color/colorPrimaryDark"
        android:textColor="#fff"
        android:id="@+id/btn19"
        android:text="Preprocessors"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn20"
            android:text="Header Files"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn21"
            android:text="Type Casting"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn22"
            android:text="Error Handling"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn23"
            android:text="Recursion"/>



    </LinearLayout>

</ScrollView>

【问题讨论】:

  • 只有最后一个?它是完全看不见的吗?
  • 只需将 android:fillViewport="true" 放入您的滚动视图中

标签: android xml android-layout android-scrollview


【解决方案1】:

用以下代码替换您的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/bar_chart_main_scroll"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn1"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:text="Overview"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn2"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:text="Program Structure"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn3"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:text="Tokens , Semicolons and Comments"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:id="@+id/btn4"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:text="Data Types"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn5"
            android:text="Variables"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn6"
            android:text="Constants and Literals"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn7"
            android:text="Operators"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn8"
            android:text="Decision Making"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn9"
            android:text="Loops"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn10"
            android:text="Functions"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn11"
            android:text="Scope Rules"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn12"
            android:text="Arrays"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:id="@+id/btn13"
            android:textColor="#fff"
            android:text="Pointers"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn14"
            android:text="Strings"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn15"
            android:text="Structures"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn16"
            android:text="Typedef"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn17"
            android:text="Input and Output"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn18"
            android:text="File I/O"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn19"
            android:text="Preprocessors"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn20"
            android:text="Header Files"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn21"
            android:text="Type Casting"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn22"
            android:text="Error Handling"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorPrimaryDark"
            android:textColor="#fff"
            android:id="@+id/btn23"
            android:text="Recursion"/>

    </LinearLayout>

</RelativeLayout>

【讨论】:

  • 希望这对你有用。请让我知道它是否有效或您需要进一步的帮助。
【解决方案2】:

尝试使用 NestedScrollView,如下所示:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:isScrollContainer="true">

............ (Your other Views)        

</android.support.v4.widget.NestedScrollView>

android:isScrollContainer="true"这很重要。

希望它有效。有任何疑问,请发表评论。

【讨论】:

    【解决方案3】:

    这是因为设置了边距,滚动视图有时会忽略它们。检查而不是第一个按钮上边距和最后一个按钮上边距,删除这两个边距并将这两个边距替换为具有相同高度的视图。这应该可以解决您的问题。

    【讨论】:

      【解决方案4】:

      尝试使用下面的代码

      <ScrollView android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:fillViewport="true"
                 xmlns:android="http://schemas.android.com/apk/res/android">
      
      <LinearLayout
          android:layout_width="match_parent"
          android:orientation="vertical"
          android:layout_height="wrap_content">
      
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/btn1"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:text="Overview"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/btn2"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:text="Program Structure"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/btn3"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:text="Tokens , Semicolons and Comments"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:id="@+id/btn4"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:text="Data Types"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn5"
              android:text="Variables"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn6"
              android:text="Constants and Literals"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn7"
              android:text="Operators"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn8"
              android:text="Decision Making"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn9"
              android:text="Loops"/>
      
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn10"
              android:text="Functions"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn11"
              android:text="Scope Rules"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn12"
              android:text="Arrays"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:id="@+id/btn13"
              android:textColor="#fff"
              android:text="Pointers"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn14"
              android:text="Strings"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn15"
              android:text="Structures"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn16"
              android:text="Typedef"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn17"
              android:text="Input and Output"/>
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn18"
              android:text="File I/O"/>
      
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn19"
              android:text="Preprocessors"/>
      
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn20"
              android:text="Header Files"/>
      
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn21"
              android:text="Type Casting"/>
      
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn22"
              android:text="Error Handling"/>
      
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@color/colorPrimaryDark"
              android:textColor="#fff"
              android:id="@+id/btn23"
              android:text="Recursion"/>
      
      
      
      </LinearLayout>
      

      我刚刚将属性“fillViewPort = “true””添加到 XML。 您可以在文档中看到,当您使用“fillViewPort”时,scrollView 会尽可能地拉伸以使视图对用户可见。 https://developer.android.com/reference/android/widget/ScrollView

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-14
        • 1970-01-01
        • 2012-02-17
        • 1970-01-01
        相关资源
        最近更新 更多