【问题标题】:Align ImageView to top right of screen将 ImageView 对齐到屏幕的右上角
【发布时间】:2013-05-20 19:03:52
【问题描述】:

我的布局中有一个 imageView,它用作不缩放的背景。但我需要将我的图像放在屏幕的右上角。我现在在屏幕的左上角有我的图像。这是我的图像视图的 xml:

    <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:src="@drawable/achtergrond"
android:scaleType="fitCenter" />

我还尝试将它放在具有重力=“顶部|右”的线性布局中,但这不会删除图片周围的空白,因为它具有 match_parent 大小。我还需要保持图片的比例,所以不能做fill_parent。

我希望有人可以帮助我!

【问题讨论】:

  • 不应该将宽度和高度设置为wrap_content吗?也许RelativeLayout 是您想要将ImageView 对齐到右上角的东西。
  • 贴出父布局的关键部分(属性)

标签: android android-layout imageview


【解决方案1】:

我想指出 hcpl 的答案是正确的,尽管我无法做到这一点,因为我已将 RelativeLayout 的重力设置为中心,因此它将每个子视图集中到中心。所以如果你来这里有同样的问题,请注意父视图。

【讨论】:

    【解决方案2】:

    可能会添加

    android:adjustViewBounds="true" 
    

    到您的ImageView?我遇到了类似的问题。

    【讨论】:

      【解决方案3】:

      以下布局会将您的图像(或任何其他视图)放置在屏幕的右上角。查看代码块下方的 cmets 了解更多详情。

      <?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" >
      
          <ImageView
              android:id="@+id/imageView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentRight="true"
              android:layout_alignParentTop="true" />
      
      </RelativeLayout>
      

      重要的部分是包装RelativeLayoutlayout_alignParentRightlayout_alignParentTop在视图本身上设置为true。

      这不会包含您的完整视图,因为它都是wrap_content。您可以use different size resources per screen type 或使用不同的layout_widthlayout_height,然后随意使用scaleType 属性(可能fitXY 缩放图像以匹配视图)。

      如果没有必要,不要使用LinearLayoutDocumentation on RelativeLayoutcan be found here.

      【讨论】:

        【解决方案4】:

        使用线性布局使图像视图适合图像使用

        android:scaleType="fitXY"
        

        【讨论】:

        • 如果我这样做,图像会得到不同的比例,所以我不能使用它。
        • 您可以使用填充进行调整
        猜你喜欢
        • 2020-06-02
        • 2017-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-09
        • 2011-05-06
        • 2015-10-16
        • 2022-06-24
        相关资源
        最近更新 更多