Cardview のコーナーの背景が透明ではありません 質問する

Cardview のコーナーの背景が透明ではありません 質問する

CardView サポート ウィジェットのカスタム実装がありますが、レイアウト ファイルに組み込むと、コーナーの背景を透明にすることができないようです。ただし、CardView サポート ウィジェットをレイアウト ファイルに配置するだけで、突然動作します。カスタム コンポーネントのコーナーを透明にするにはどうすればよいでしょうか。

例

これは、CardView のカスタム実装のレイアウト ファイルです。

ビューカード.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>

スタイル.xml

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

これは、2 つの CardView を含むレイアウト ファイルです。最初のファイルは白い角があり、2 番目のファイルは基本的に view_card.xml と同じレイアウトですが、白い角がありません (透明)。

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

アップデート1

Just89 の解決策を試しましたが、Android の下位バージョンではクラッシュが発生します。

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

簡単に検索してみたところ、次の投稿を見つけました。android.graphics.drawable.ColorDrawable は android.support.v7.widget.RoundRectDrawableWithShadow にキャストできません

回答では、以下を使用して背景色を設定することを提案しています。setCardBackgroundColorただし、これにより白い角が戻ってしまいます。

アップデート2

受け入れられた回答はこの問題を解決しますが、推奨される解決策ではありません。カスタムCardViewコンポーネントを作成するときにミスを犯し、白い角が発生しました。確認してくださいこれ何が間違っていたのかを確認するために答えてください。

ベストアンサー1

カスタム実装内のコンテキストが利用可能な場所で、次のコードを使用します。

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

編集:

上記のクラッシュを回避するには、Lollipop より前の Android バージョンでは次のコードを使用します。

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

おすすめ記事