繁体   English   中英

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

[英]Android - How to make one view's center on another view's top right corner inside list item layout

在GridView中,我正在显示书籍网格。 每本书都有状态-新书,收藏夹,已完成。 这是示例,我要实现:

在此处输入图片说明

我的版式有2个ImageViews: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>

我发现如何定位一个视图的另一个视图的右上角中心在这里 在该示例中,解决方案是针对一项-线性布局。 就我而言,我将状态图标放在网格视图项目的一角。 结果,大多数状态图标不可见:

在此处输入图片说明

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

为了提高效率,您可以使用比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大小以“增加材料”(如果需要中等大小,则使用x4)。 而不是95dp使用96,而不是146使用144或152 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM