2015-09-02 24 views
7

Tôi muốn có một CardView có chứa một ImageView chồng lên đường biên trái của CardView. Tôi muốn làm điều này bằng cách cho ImageView một biên độ âm. Điều này làm việc tốt với tất cả các bố cục khác (LinearLayout/RelativeLayout/FrameLayout) bằng cách đặt clipChildren = "false".CardView bỏ qua android: clipChildren = "false"

Tuy nhiên tôi không thể làm cho nó hoạt động với một CardView. ImageView sẽ được cắt bớt và không trùng lặp với CardView.

<android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:clipChildren="false"> 
    <ImageView 
      android:layout_marginLeft="-10dp" 
      android:background="@drawable/some_picture" 
      android:layout_width="50dp" 
      android:layout_height="50dp"/> 
</android.support.v7.widget.CardView> 
+0

lý do tại sao bạn không đặt hình ảnh bên ngoài thẻView và sau đó đặt cả hai hình ảnh trong Relati veLayout chồng lên nhau? –

+1

Sẽ là một lựa chọn. Tuy nhiên có ImageView bên trong của CardView cảm thấy giống như giải pháp dọn dẹp cho tôi. – tymm

+0

Kiểm tra liên kết của anh ấy, hy vọng nó sẽ giúp ... http://stackoverflow.com/questions/14845891/how-to-achieve-android-ui-like-this-image-layout-about-androidclipchildren –

Trả lời

5

Ok, điều này dường như là một vấn đề duy nhất với Android 5+, giải pháp là để thiết lập

cardView.setClipToOutline(false); 

Nguồn - https://www.reddit.com/r/androiddev/comments/2tccge/cant_draw_outside_of_cardview_parent/

+4

Không phải vấn đề chỉ với 5.0, phiên bản thấp hơn cũng không hoạt động. Giải pháp này chỉ hỗ trợ api cấp 21 trở lên. Giải pháp cho phiên bản dưới đây là gì. –

1

bọc nó trong LinearLayout với param LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:clipChildren="false"> 

<android.support.v7.widget.CardView/> 

</LinearLayout> 
Các vấn đề liên quan