【问题标题】:How to make my layout able to scroll down?如何使我的布局能够向下滚动?
【发布时间】:2010-09-29 06:17:08
【问题描述】:

我无法向下滚动屏幕以查看“回复者:”部分中的数据。如何使我的布局可滚动?

【问题讨论】:

    标签: android layout


    【解决方案1】:

    只需将所有内容包含在 ScrollView 中:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
        <!-- Here you put the rest of your current view-->
    </ScrollView>
    

    正如 David Hedlund 所说,ScrollView 只能包含一项......所以如果你有这样的东西:

    <?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">
        <!-- bla bla bla-->
    </LinearLayout>
    

    您必须将其更改为:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
        <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
            <!-- bla bla bla-->
        </LinearLayout>
    </ScrollView>
    

    【讨论】:

    • +1。快速说明:ScrollView 只能包含一个孩子,因此如果您当前有很多视图,则需要将它们包装在一个视图组中(例如 LinearLayout
    • 如何解决我的问题请帮助我stackoverflow.com/questions/38588702/…
    【解决方案2】:

    使用滚动视图和相对布局:

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"> <!--IMPORTANT otherwise backgrnd img. will not fill the whole screen -->
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:background="@drawable/background_image"
        >
    
        <!-- Bla Bla Bla i.e. Your Textviews/Buttons etc. -->
        </RelativeLayout>
    </ScrollView>
    

    【讨论】:

    • 什么是视口标签
    【解决方案3】:

    只需将所有内容包装在 ScrollView 中

    <?xml version="1.0" encoding="utf-8"?>
    
    <ScrollView 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"
        tools:context="com.ruatech.sanikamal.justjava.MainActivity">
    <!-- Here you put the rest of your current view-->
    </ScrollView>
    

    【讨论】:

      【解决方案4】:

      是的,非常简单。只需将您的代码放入其中:

      <androidx.core.widget.NestedScrollView
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent" 
          android:layout_height="match_parent">
      
      //YOUR CODE
      
      </androidx.core.widget.NestedScrollView>
      

      【讨论】:

        【解决方案5】:

        如果您在执行上述操作后甚至没有滚动......

        设置android:layout_height="250dp"或者你可以说xdp,其中x可以是任何数值。

        【讨论】:

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