【问题标题】:Android ScrollView and buttons at bottom of the screenAndroid ScrollView 和屏幕底部的按钮
【发布时间】:2012-03-08 17:42:13
【问题描述】:

我想实现这个:一个包含许多元素(ImageViews、TextViews、EditTexts 等)的 ScrollView,然后在 ScrollView 之后,一些按钮(它们是自定义 ImageViews)总是恰好出现在屏幕底部。

如果我使用 android:fillViewport="true" 属性,那么如果 ScrollView 的元素太大而无法适应屏幕尺寸,按钮就会变得不可见。如果我使用 android:Weight=1 属性,那么当屏幕很大并且可以容纳时,ScrollView 只能获得屏幕的 50%(我希望按钮占一小部分,大约 10%)。如果我将 android:Weight 设置为更大的值,那么按钮会显得非常小。

请帮忙!也许这是我忽略的简单事情,但我已经敲了好几个小时了!

【问题讨论】:

    标签: android android-layout scrollview


    【解决方案1】:

    刚刚创建并测试了它。看起来像你想要的。

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
        <LinearLayout
                android:id="@+id/buttons"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center"
                android:layout_alignParentBottom="true">
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Custom Button1"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Custom Button2"/>
        </LinearLayout>
    
        <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@id/buttons">
             <!--Scrollable content here-->
            <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">                
                <TextView
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"
                        android:text="test text"
                        android:textSize="40dp"/>
            </LinearLayout>
        </ScrollView>
    
    </RelativeLayout>
    

    【讨论】:

    • 我遇到了类似的问题。与此解决方案配合得很好
    • 当 ScrollView 填满时,它不会覆盖按钮吗?
    • @aleksey-masny:我有类似的问题,但我想使用 TextEdit 而不是 TextView,类似于所见即所得的编辑器粗体斜体和...按钮位于屏幕底部和键盘上方。我按照你说的做了,但我无法用 TextEdit 填充屏幕。它说类似elements in ScrollView must have wrap_content as their height value!你可以帮帮我吗!谢谢
    • 嗨,当键盘到来时,输入字段和页脚的页眉和滚动视图的布局也向上滚动。如何在底部修复页脚。请帮助我
    • 就是这样!完美的!谢啦!这是相对布局的基本实现,但非常重要。
    【解决方案2】:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <TextView 
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Hello World"
                />
                <TextView 
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Hallo Welt"
                />       
            </LinearLayout>
        </ScrollView>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Go next page"
                android:layout_alignParentRight="true" />
        </RelativeLayout>
    
    </LinearLayout>
    

    【讨论】:

    • 嗨,当键盘到来时,输入字段和页脚的页眉和滚动视图的布局也向上滚动。如何在底部修复页脚。请帮助我
    • 它奏效了,过去 3 小时我一直在努力寻找解决方案。
    【解决方案3】:

    这对我有用。给滚动视图赋予 1 的权重。将滚动视图后面的所有其他小部件放入布局中。滚动视图将增长到足以不阻塞其余部分。

    Widgets in scroll view and rest at bottom

    【讨论】:

      【解决方案4】:

      scrollview 无法适应屏幕,因为你把它放在线性布局上,所以线性布局适合屏幕,

      尝试将滚动视图作为 xml 布局的根元素

      <?xml version="1.0" encoding="utf-8"?>
      <ScrollView
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/scrollView1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" >
      
          <LinearLayout
              android:id="@+id/linearLayout1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" >
              <!-- Here you can put some XML stuff and BOOM! your screen fit to scrollview -->
      
          </LinearLayout>
      </ScrollView>
      

      【讨论】:

        【解决方案5】:

        如果不想使用RelativeLayout,最好使用LinearLayout。我认为这种方法更好。

        只需将 layout_weight 设置为 1

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-07-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-02-02
          • 1970-01-01
          • 2014-11-01
          相关资源
          最近更新 更多