【问题标题】:ImageView won't fill parentImageView 不会填充父级
【发布时间】:2012-07-15 16:25:08
【问题描述】:

我的一个屏幕上有一个 ScrollView。我希望右边缘有阴影。我决定最简单的方法是让 ScrollView 的子级成为 RelativeLayout,并拥有两个 RelativeLayout 的子级——一个是 LinearLayout,它将容纳屏幕的布局,第二个 View 是阴影。

就这样……

<ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scrollbars="none" >

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                <!-- stuff -->
                </LinearLayout>

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:src="@drawable/shadow"
                    android:layout_alignParentRight="true"
                    />
            </RelativeLayout>
        </ScrollView>

不幸的是,这并不完全奏效。 ImageView 强制其尺寸为图像文件的大小。它不会垂直拉伸到RelativeLayout 的高度。我也试过“match_parent”无济于事。图像是 9-patch。

想法?

【问题讨论】:

    标签: android scrollview android-relativelayout


    【解决方案1】:

    您希望图像右侧有一个阴影,然后使用带有Horizontal Orientation 的单一布局,您决定使用相对布局是件好事。使用

    android:orientation="vertical"
    

    在此布局中,添加这两个图像。如果你还有疑问,给我那两张图片或示例图片,我会给你代码

    【讨论】:

      【解决方案2】:

      试试这个

      <ImageView
                      android:layout_width="wrap_content"
                      android:layout_height="fill_parent"
                      android:background="@drawable/ic_launcher"
                      android:scaleType="fitXY"
                      android:layout_alignParentRight="true"
                      />
      

      这就是我得到的

      和代码 id

      <?xml version="1.0" encoding="utf-8"?>
      

      <ScrollView
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:fillViewport="true"
          android:scrollbars="none" >
      
          <RelativeLayout
              android:layout_width="fill_parent"
              android:layout_height="wrap_content" >
      
              <LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical" >
      
                  <!-- stuff -->
              </LinearLayout>
      
              <ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="fill_parent"
                  android:layout_alignParentRight="true"
                  android:background="@drawable/ic_launcher"
                  android:scaleType="fitXY" />
          </RelativeLayout>
      </ScrollView>
      

      【讨论】:

      • scaleType 没有解决问题。为了确保 ImageView 的大小不正确,我将背景更改为红色。 ImageView 只是设置为 src 的图像的高度。它似乎对成为 match_parent 或 fill_parent 不感兴趣。
      • 你注意到我把src改成了background吗? :) 使用src 你永远无法实现你想要的。
      • 将 src 切换到后台也不起作用。事实上,我已经用普通的 View 和 LinearLayout 而不是 ImageView 尝试过同样的事情。
      • 这也将android:fillViewport="true" 放入您的滚动视图中。
      • 别人已经推荐了fillViewport,但是并没有解决问题。
      【解决方案3】:

      您的问题与ImageView 或9 补丁本身无关,而是与您将所有内容都包装在ScrollView 中这一事实有关。 ScrollView 将自动强制其 children 直接子级包装其内容,无论您将其告知FILL_PARENT 还是MATCH_PARENT - 顺便说一句,两者都做同样的事情;唯一的区别是名称,它更好地反映了标志的实际行为。

      幸运的是ScrollView 提供了一种方法来强制它用标志填充视口,这将使行为非常类似于将FILL_PARENT 设置为常规视图。添加属性android:fillViewport 或使用代码中的setFillViewport()

      编辑:为了清楚起见,您需要在ScrollView 上设置该标志。另外,如果是ScrollView应该有阴影,你能不能不把你的9补丁作为背景发送给它?我想这确实取决于您的实际图像的外观。关于您的评论:是的,RelativeLayout 在定位和调整孩子大小方面是灵活的,但任何孩子仍然会被绑定到其父母的大小。

      我确实感觉我们中的一些人可能正在朝着与您的想法不同的方向努力。用一张简单的图画肯定会有助于澄清事情。

      【讨论】:

      • 不幸的是,fillViewport 没有解决我的问题。此外,ImageView 不是 ScrollView 的直接子项。相对布局是。然后我应该能够制作 RelativeLayout 填充父/匹配父的任何子。
      • @MH。您所说的是正确的,但仅限于ScrollView 的孩子,这里是RelativeLayout。此约束不会超出层次结构链的范围。
      • @Devunwired:抱歉之前的措辞,我应该更小心一点,说“它的直接孩子”而不是“孩子”。在我的答案中修复。我仍然认为fillViewport 可以解决您的问题,但为了避免混淆,如果 TS 可以模拟一个小图就好了。
      【解决方案4】:

      应用可绘制内容作为ImageView 的来源在某种程度上带有一个内在要求,即您希望视图尽其所能容纳内容,而无需对内容本身进行太多修改。通常,这是您希望 ImageView 出现的行为。

      您真正想要的是通过将可绘制内容设置为视图的background 获得的行为,您根本不需要ImageView。背景旨在简单地拉伸、填充等到视图的任何大小。此外,由于您使用的是RelativeLayout,因此您可以通过添加id 和一些额外的layout_align参数来告诉视图与您正在遮蔽的视图的边界相匹配。

              <RelativeLayout
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content" >
      
                  <LinearLayout
                      android:id="@+id/content_layout"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:orientation="vertical" >
                  <!-- stuff -->
                  </LinearLayout>
      
                  <View
                      android:layout_width="11dp"
                      android:layout_height="fill_parent"
                      android:layout_alignParentRight="true"
                      android:layout_alignTop="@id/content_layout"
                      android:layout_alignBottom="@id/content_layout"
                      android:background="@drawable/shadow"
                      />
              </RelativeLayout>
      

      【讨论】:

      • 我已经尝试过了(虽然我必须将宽度指定为 11dip,否则根本不显示)无济于事
      • 编辑,有个更好的主意。您在固定尺寸上是正确的,内部没有任何内容(其中背景不算在内),视图无法正确测量 wrap_content
      • 我唯一能想到的就是在代码中抓取View,获取屏幕高度,将View的高度设置为相同。
      • 上面的例子也不行吗? alignTop 和 alignBottom 属性应该为您处理所有这些。
      • 是的,alignTop 与 alignBottom 结合使用。我在之前的评论中提出的解决方案也是如此;但你的更简单。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多