【问题标题】:RelativeLayout issue - image overlaps ScrollViewRelativeLayout 问题 - 图像与 ScrollView 重叠
【发布时间】:2011-09-16 09:50:05
【问题描述】:

我试图在我的应用底部贴一张图片,上面是我有一个滚动视图,如下所示:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:id="@+id/RelativeLayout1">
        <ScrollView  android:paddingTop="180dip" android:layout_width="fill_parent" android:id="@+id/scrollView12" android:layout_height="wrap_content">
            <LinearLayout android:id="@+id/linearLayout4" android:layout_width="fill_parent" android:paddingRight="8dip" android:orientation="vertical" android:layout_height="wrap_content">
            </LinearLayout>
        </ScrollView>
        <ImageView android:layout_width="fill_parent"
            android:background="@color/cinza" android:src="@drawable/header"
            android:id="@+id/imageView2" android:layout_height="wrap_content" android:layout_alignParentBottom="true">
        </ImageView>
</RelativeLayout>

我动态地提供滚动视图,因此它的大小可以改变。我的问题是,如果它一直增长到显示结束,最后的记录就会出现在我的图片后面。我希望它长到这张照片。 关于如何做到这一点的一些线索?尝试了几种方式,relativelayouts,linearlayouts,但是有时候图片消失,有时候不粘在底部...

【问题讨论】:

    标签: android android-relativelayout


    【解决方案1】:

    您应该为 ScrollView 添加 android:layout_above="@+id/imageView2",并首先定义 ImageView,因为您希望它首先被绘制,然后是 RelativeLayout 中的 ScrollView。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/RelativeLayout1">
        <ImageView
            android:layout_width="fill_parent"
            android:background="@color/cinza"
            android:src="@drawable/header"
            android:id="@+id/imageView2"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">
        </ImageView>
        <ScrollView
            android:paddingTop="180dip"
            android:layout_width="fill_parent"
            android:id="@+id/scrollView12"
            android:layout_above="@+id/imageView2"
            android:layout_height="wrap_content">
            <LinearLayout
                android:id="@+id/linearLayout4"
                android:layout_width="fill_parent"
                android:paddingRight="8dip"
                android:orientation="vertical"
                android:layout_height="wrap_content">
            </LinearLayout>
        </ScrollView>   
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      RelativeLayout 在这种情况下不是正确的选择。必须要用LinearLayout,关键是android:layout_weight参数:

      <?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:paddingTop="180dip" 
              android:layout_width="fill_parent" 
              android:layout_height="0dp"
              android:layout_weight="1">
      
              <LinearLayout android:id="@+id/linearLayout4" 
                  android:layout_width="fill_parent" 
                  android:paddingRight="8dip" 
                  android:orientation="vertical" 
                  android:layout_height="wrap_content">
              </LinearLayout>
          </ScrollView>
      
      <ImageView android:layout_width="fill_parent"
          android:background="@color/cinza" 
          android:src="@drawable/header"
          android:id="@+id/imageView2" 
          android:layout_height="wrap_content" />
      

      【讨论】:

      • 这完全有效,非常感谢!!!但是我不太了解 layout_weight 是如何工作的,我会读一下。
      • RelativeLayout 是一个不错的选择,你只需要了解它是如何工作的。对于像这样的简单案例,LinearLayout 确实也可以工作,但是例如,如果你想在底部有两张图片,一张靠近另一张,你应该将它们包装在一个新的 LinearLayout 中,而使用RelativeLayout,您仍然只能拥有一个父母。在这里阅读更多:Layout Tricks: Creating Efficient Layouts
      • 那个人回答了你的问题,而你没有将其标记为答案?跛脚。
      【解决方案3】:

      给 scoolview 一个 id 然后在图像视图中设置一个标签 android:layout_blow="id of scrollview"

      【讨论】:

      • 我试过了,图像消失了,这不是答案,至少在这种情况下
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多