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
Đâ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.
http://stackoverflow.com/questions/29342633/cardview-corner-radius –
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