Cardview Eckhintergrund nicht transparent
Ich habe eine benutzerdefinierte Implementierung eines CardView-Upport-Widgets, aber ich kann den Hintergrund für die Ecken nicht transparent machen, wenn ich ihn in meine Layout-Datei einbinde. Wenn ich jedoch einfach das CardView-Support-Widget in meine Layout-Datei lege, funktioniert es plötzlich. Wie kann ich die Ecken für meine benutzerdefinierte Komponente transparent machen?
Dies ist die Layoutdatei für meine benutzerdefinierte Implementierung von 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>
Und dies ist meine Layoutdatei, die die beiden CardViews enthält. Das erste mit den weißen Ecken und das zweite, das im Grunde das gleiche Layout wie view_card.xml hat, aber ohne die weißen Ecken (transparent).
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>
Update 1
Ich habe die Lösung von Just89 ausprobiert, sie führt jedoch bei niedrigeren Android-Versionen zu einem Absturz.
android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow
Nach einer kurzen Suche habe ich folgenden Beitrag gefunden. android.graphics.drawable.ColorDrawable kann nicht in android.support.v7.widget.RoundRectDrawableWithShadow @ umgewandelt werd
Die Antwort schlägt vor, die Hintergrundfarbe wie folgt einzustellen:setCardBackgroundColor
. Dies wird jedoch die weißen Ecken zurückbringen.
Update 2
Die akzeptierte Antwort löst dieses Problem, ist jedoch nicht die bevorzugte Lösung. Beim Erstellen der benutzerdefinierten CardView-Komponente, die diese weißen Ecken verursacht hat, ist ein Fehler aufgetreten. PrüfenDie antworte um zu sehen was ich falsch gemacht habe.