6

Tôi có một triển khai tùy chỉnh của một tiện ích hỗ trợ CardView nhưng tôi dường như không thể có nền cho các góc trong suốt khi tôi đưa nó vào tệp bố cục của mình. Tuy nhiên, nếu tôi chỉ đơn giản đặt tiện ích hỗ trợ CardView vào tệp bố cục của tôi, nó đột nhiên hoạt động. Làm cách nào để tôi có được các góc trong suốt cho thành phần tùy chỉnh của mình?Góc nền góc nhìn không trong suốt

example

Đây là file layout để thực hiện tùy chỉnh của tôi về CardView:

view_card.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/view_card" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    style="@style/Custom.Widget.CardView"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="@dimen/default_padding"> 

    <TextView 
     android:id="@+id/view_mainText" 
     style="@style/Custom.Widget.TextView.Header" 
     android:textColor="@color/instruction_balloon_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/view_subText" 
     android:textSize="@dimen/text_size_medium" 
     android:textColor="@color/instruction_balloon_text" 
     android:singleLine="false" 
     android:text="Please remove white corners :-(" 
     android:textIsSelectable="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

styles.xml

<style name="Custom.Widget.CardView" parent="CardView"> 
    <item name="cardBackgroundColor">@color/card_backgroundColor</item> 
    <item name="cardCornerRadius">12dp</item> 
</style> 

Và đây là tệp bố cục của tôi bao gồm hai CardView. Cái đầu tiên với các góc trắng và góc thứ hai về cơ bản là bố cục giống như view_card.xml nhưng không có các góc trắng (trong suốt).

example.xml

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <some.private.namespace.CardView 
     android:id="@+id/custom_card_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/default_margin" /> 

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/view_card" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/default_margin" 
     style="@style/Custom.Widget.CardView"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:padding="@dimen/default_padding"> 

      <TextView 
       android:id="@+id/view_mainText" 
       style="@style/Custom.Widget.TextView.Header" 
       android:textColor="@color/instruction_balloon_text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

      <TextView 
       android:id="@+id/view_subText" 
       android:textSize="@dimen/text_size_medium" 
       android:textColor="@color/instruction_balloon_text" 
       android:singleLine="false" 
       android:text="I have no white corners :-)" 
       android:textIsSelectable="true" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
     </LinearLayout> 
    </android.support.v7.widget.CardView> 
    ... some other views 
</LinearLayout> 

Cập nhật 1

tôi đã cố gắng giải pháp Just89, tuy nhiên nó kết quả trong một vụ tai nạn trên các phiên bản Android thấp hơn.

android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow 

Sau khi tìm kiếm nhanh, tôi tìm thấy bài đăng sau. android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow

Câu trả lời đề xuất đặt màu nền bằng cách sử dụng: setCardBackgroundColor. Tuy nhiên, điều này sẽ mang lại các góc trắng.

Cập nhật 2

Câu trả lời được chấp nhận sẽ giải quyết vấn đề này, tuy nhiên nó không phải là giải pháp ưa thích. Tôi đã phạm sai lầm khi tạo thành phần CardView tùy chỉnh đã gây ra các góc trắng này. Hãy xem this câu trả lời để xem tôi đã làm gì sai.

+0

http://stackoverflow.com/questions/29342633/cardview-corner-radius –

+0

Không chắc chắn điều đó sẽ giúp giải quyết vấn đề của tôi như thế nào. – Wirling

Trả lời

16

Sử dụng đoạn mã sau, vào một nơi mà bối cảnh có sẵn, trong việc thực hiện tùy chỉnh của bạn:

setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent)); 

EDIT:

Sử dụng đoạn mã sau cho các phiên bản Android thấp hơn sau đó Lollipop để tránh đề cập tai nạn.

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)) 
    { 
    getBackground().setAlpha(0); 
    } 
    else 
    { 
    setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent)); 
    } 
+0

Điều này đã thực hiện công việc. Cảm ơn người đàn ông, đã cứu tôi rất nhiều thời gian! – Wirling

+0

Thật không may điều này sẽ dẫn đến sự cố trên một số thiết bị. android.graphics.drawable.ColorDrawable không thể truyền tới android.support.v7.widget.RoundRectDrawableWithShadow. Tôi sẽ cập nhật câu hỏi của tôi với một số phát hiện. – Wirling

+0

Tôi đã chỉnh sửa câu trả lời của mình với giải pháp cho vụ tai nạn – Just89

0

Bạn có thể thử lồng màn hình góc tròn bên trong một chế độ xem/bố cục khác. Giao diện bên ngoài ở đây có thể có nền trong suốt, để ngay cả khi các góc tròn của khoảng trống bên trong để lại không gian trên các góc, sẽ không nhìn thấy được vì khung nhìn bên ngoài có màu nền trong suốt. Something như thế này:

<RelativeLayout> 
android:background = "@color/transparent" 
< some.private.namespace.CardView 
android:margin="8dp" 
..... 
/> 
</RelativeLayout> 
+0

Tôi đã thử điều đó và nó không hoạt động. – Wirling

+0

Đầu ra bạn có được bằng cách làm điều này là gì? Nó giống như hình ảnh đầu tiên hay cái gì khác? –

+0

Kết quả giống với hình ảnh đầu tiên. – Wirling

1

Dường như bạn đang tạo ra một tùy chỉnh CardView tạo file layout của bạn vì điều này

<some.private.namespace.CardView 
     android:id="@+id/custom_card_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/default_margin" /> 

CardView của bạn có thể được mở rộng một cái gì đó (nói LinearLayout) và bạn có thể tạo ra một chế độ xem con khác bên trong Chế độ xem chính.Vì vậy, chỉ cần đặt bố mẹ ngay lập tức của bố cục thẻ thành minh bạch bằng cách sử dụng

setBackground();

Có thể điều này sẽ hữu ích.

0

thêm phong cách mới vào tập tin styles.xml tương tự này:

<style name="CardViewRadius"> 
    <item name="cardBackgroundColor">@color/colorGray</item> 
    <item name="cardCornerRadius">18dp</item> 
</style> 

và sử dụng:

<android.support.v7.widget.CardView 
     style="@style/CardViewRadius" 
     android:layout_marginTop="15dip" 
     android:layout_width="200dp" 
     android:layout_height="100dp" 
     android:layout_gravity="center_horizontal"/> 

và kết quả: enter image description here

0

Sau một vài năm, tôi đã đến kết luận rằng tôi đã mắc sai lầm cơ bản khi tạo ra hợp chất các thành phần.

Khi tạo thành phần hợp chất mở rộng android.support.v7.widget.CardView bố cục xml được tăng cao cũng không được chứa thẻ CardView. Về cơ bản bạn sẽ chỉ cần thêm một CardView để CardView khác mà sẽ dẫn đến các góc trắng phía sau CardView tùy chỉnh của tôi.

accepted answer sẽ giải quyết vấn đề này. Tuy nhiên nó không phải là giải pháp hoàn hảo.

Có 2 cách để giải quyết vấn đề thực sự:

xem
  1. Hợp chất kéo dài android.support.v7.widget.CardView và xml bố trí không chứa android.support.v7.widget.CardView nhưng thay vào đó LinearLayout là gốc. Tạo kiểu sẽ phải xử lý trong lớp học.
  2. Chế độ xem tổng hợp mở rộng LinearLayout và bố cục xml chứa android.support.v7.widget.CardView làm gốc.

Tôi đã chọn giải pháp đầu tiên để khắc phục vấn đề này. Tôi hy vọng điều này sẽ giúp mọi người mắc sai lầm tương tự.

Các vấn đề liên quan