Android - 如何使一个 View 的中心位于列表项布局内另一个 View 的右上角

标签 android android-layout

在 GridView 中,我显示了书籍网格。每本书都有状态——新的、最喜欢的、已完成的。这是我想要实现的示例:

enter image description here

我的布局有 2 个 ImageView:1 个用于书籍封面,1 个用于书籍状态图标(新、完成、收藏)

        <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/ivCover"
            android:layout_width="95dp"
            android:layout_height="146dp"
            />
        <ImageView
            android:layout_width="26dp"
            android:layout_height="26dp"
            android:src="@drawable/icon_new"
            android:layout_alignParentTop="@id/ivCover"
            android:layout_alignRight="@id/ivCover"
            android:background="@drawable/shape_round_book_status"
            android:layout_marginRight="-13dp"
            android:layout_marginTop="-13dp"/>
    </RelativeLayout>

我找到了如何将一个 View 的中心定位在另一个 View 的右上角here 。在该示例中,解决方案是针对一项 - 线性布局。就我而言,我将状态图标放在 GridView 项目的角落上。结果,大部分状态图标不可见:

enter image description here

如何在列表项布局内使一个 View 的中心位于另一个 View 的右上角?

最佳答案

为了提高效率,您可以使用比 RelativeLayout 性能更好的 FrameLayout 来完成此操作。

也就是说,实现这一点的技巧是在书籍图像中添加边距以模拟徽章退出图像。比如像这样

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/ivCover"
        android:layout_width="95dp"
        android:layout_height="146dp"
        android:layout_marginTop="30dp"
        android:layout_marginRight="30dp"
        />

    <ImageView
        android:layout_width="26dp"
        android:layout_height="26dp"
        android:src="@drawable/icon_new"
        android:layout_gravity="top|right"
        android:background="@drawable/shape_round_book_status"/>

</FrameLayout>

调整布局边距以适合您的设计。

** 其他小建议是使用 x8 尺寸来“更多 Material ”(如果您需要中等尺寸,则使用 x4)。使用 96 代替 95dp,使用 144 或 152 代替 146...

关于Android - 如何使一个 View 的中心位于列表项布局内另一个 View 的右上角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32166791/

相关文章:

Android:从底部导航中删除阴影

android - 截取 LinearLayout 和 RecyclerView 的屏幕截图

java - Opengl es 与 obj 加载器失真

android - 在 WebView 中滚动到 HTML 内容

java - 在 Android 线程内部(和外部)使用类变量

android - 微调器,如果我不点击则不设置值(android)

java - 以编程方式编辑自定义 View

java - Android从末尾读取文本文件

java - 文本观察者问题

android - 如何在不使用新图像的情况下减小Android单选按钮上显示的图像大小