【问题标题】:How can I place new items under scrollview如何在滚动视图下放置新项目
【发布时间】:2011-11-30 14:15:57
【问题描述】:

我有一个包含很多项目的垂直可滚动布局,工作正常。
我正在尝试在屏幕底部放置一个新的线性布局,它不会成为可滚动布局的一部分。
也就是说,它将位于独立于可滚动部分的按钮上(如广告视图)。 我只能将它放在滚动视图中。
我怎样才能把它放在下面,让它总是可见?

【问题讨论】:

    标签: android layout limit scrollable


    【解决方案1】:

    使用RelativeLayout,并像这样组织它:

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <ScrollView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:layout_centerHorizontal="true"
            android:layout_alignParentTop="true"
            android:layout_above="@+id/linearLayoutThatDoesNotScroll" >
    
            <LinearLayout
                android:id="@+id/linearLayoutWithLotofContent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
            </LinearLayout>
    
        </ScrollView>
    
        <LinearLayout
            android:id="@+id/linearLayoutThatDoesNotScroll"  
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" >
        </LinearLayout>
    
    </RelativeLayout>
    

    诀窍在于 ScrollView 的放置,同时它与屏幕顶部对齐,并且位于下方固定的 LinearLayout 上方。它只是工作。

    【讨论】:

    • 我们不能用LinearLayout代替Relative吗?
    【解决方案2】:

    类似这样的:)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" >
        <ScrollView android:id="@+id/scroll_view"
            android:layout_width="fill_parent" 
            android:layout_height="0dp"
            android:layout_weight="1" >
            <LinearLayout android:id="@+id/content"
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" />
        </ScrollView>
        <LinearLayout android:id="@+id/bottom"
            android:layout_width="fill_parent" 
            android:layout_height="10dip" />          
    </LinearLayout>
    

    您将使用 android:id=bottom 将底部内容添加到底部 linearLayout :)

    【讨论】:

    • 这行不通。由于您为滚动视图为 fill_parent 提供了高度,因此它将占据整个屏幕,并且滚动视图之后的 LinearLayout 将一无所有。
    • 它有效。我认为这比使用 RelativeLayout 容易得多。
    【解决方案3】:

    如果您使用垂直的LinearLayout 来保持可滚动布局,那么您可以将广告视图添加到可滚动布局下方的LinearLayout,它会出现在屏幕底部。 (假设你的权重设置正确,并且滚动布局设置为WRAP_CONTENT

    RelativeLayout 允许您将 adview 设置为与可滚动布局的底部对齐,但您仍需要确保可滚动布局设置为 WRAP_CONTENT,因此它不会自动占据整个屏幕。

    【讨论】:

      【解决方案4】:
      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" >
      
          <Button
              android:id="@+id/dialogButtonOK"
              android:layout_width="100px"
              android:layout_height="wrap_content"
              android:text=" Ok "
              android:layout_alignParentBottom="true"
              android:layout_centerInParent="true"
              android:background="@drawable/button_style"
              />
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="5dp"
              android:id="@+id/scrView"
              android:layout_above="@id/dialogButtonOK"
              android:layout_alignParentTop="true"
             >
                  <HorizontalScrollView
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content" >
                            <TableLayout
                              android:layout_width="fill_parent"
                              android:layout_height="wrap_content"
                              android:stretchColumns="*"
                              android:id="@+id/main_table" >
                          </TableLayout>
                  </HorizontalScrollView>
          </ScrollView>
      
      
      
      
      </RelativeLayout>
      

      这对我有用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-18
        • 1970-01-01
        • 1970-01-01
        • 2017-02-23
        • 1970-01-01
        • 2011-07-08
        相关资源
        最近更新 更多