【问题标题】:Can't align image view properly in relative layout无法在相对布局中正确对齐图像视图
【发布时间】:2014-06-06 00:50:02
【问题描述】:

我想在相对布局中显示与顶部对齐的图像,阴影应该在图像周围而不是在布局周围。出了什么问题我不明白。

<RelativeLayout
        android:layout_width="117dp"
        android:layout_height="162dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:src="@drawable/rectangle"
            android:background="@drawable/drop_shadow"/>
</RelativeLayout>

这是在设计模式下预览时图像的外观。注意由于阴影而出现的白色背景,看起来即使实际图像更小,但图像视图正在使用相对布局参数来投影。任何帮助表示赞赏。

【问题讨论】:

  • 大胆猜测:您的容器对于图像来说太小了。
  • 试试 android:adjustViewBounds="true" 和/或 android:scaleType="fitStart" 看看它们是否有什么不同。
  • @bstar55 android:adjustViewBounds="true" 确实让我得到了预期的结果。我之前尝试过,但我想我在玩很多东西 :) 再次感谢...感谢 Der Golem 以及花时间回复。
  • @lazyguy 很高兴听到这个消息!我将其移至下面的答案中以供接受:)

标签: android android-layout


【解决方案1】:
  <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/selector">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher" />

 </RelativeLayout>
  • 这里选择器文件:

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
        <stroke       
              android:width="1dp"
              android:color="#000000" />
    </shape>
    

希望这可能会有所帮助。

【讨论】:

    【解决方案2】:

    看起来您只需要添加 adjustViewBounds 属性。该属性默认为 false...

    <RelativeLayout
        android:layout_width="117dp"
        android:layout_height="162dp">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:adjustViewBounds="true" <-- ADD THIS
            android:src="@drawable/rectangle"
            android:background="@drawable/drop_shadow"/>
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多