2015-08-23 40 views
1

在GridView中,我顯示了書籍的網格。每本書都有新的,最喜歡的,完成的。這就是例子,我想實現的東西:Android - 如何使一個視圖的中心位於列表項佈局的另一個視圖的右上角

enter image description here

我的佈局有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> 

我發現如何將一個視圖的中心定位在另一個視圖的右上角here。在這個例子中,解決方案是針對一個項目 - 線性佈局。就我而言,我在網格視圖項目的角落放置了狀態圖標。在結果,大多數狀態圖標不可見:

enter image description here

如何使一個視圖的列表項佈局內的另一個視圖的右上角中心?

回答

4

爲了更有效率,您可以使用FrameLayout來做到這一點,它的性能比RelativeLayout要好。

說這樣做的訣竅是在書籍圖像中放置一個邊距以模擬徽章退出圖像。例如像這樣

<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 ...

+1

非常感謝您的回答!這工作!我做了marginTop和marginRight 10dp!並感謝您的建議! –

相關問題