android - Cardview角落背景不透明

标签 android android-layout android-support-library android-custom-view android-cardview

我有一个 CardView 支持小部件的自定义实现,但是当我将它包含在我的布局文件中时,我似乎无法使角落的背景透明。但是,如果我只是将 CardView 支持小部件放在我的布局文件中,它就会突然起作用。如何让我的自定义组件的角透明?

example

这是我自定义实现 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>

样式.xml

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

这是我的布局文件,其中包含两个 CardView。第一个有白角,第二个与 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 cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow

答案建议使用:setCardBackgroundColor 设置背景颜色。 然而,这将带回白色角落。

更新 2

接受的答案将解决此问题,但它不是首选解决方案。我在创建导致这些白角的自定义 CardView 组件时犯了一个错误。检查this回答看看我做错了什么。

最佳答案

在您的自定义实现中上下文可用的地方使用以下代码:

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);
  }

关于android - Cardview角落背景不透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34810447/

相关文章:

android - 用于后台任务的最佳做法是什么?

java - 如何在android中使用Camera.Parameter以黑白模式打开并捕获相机图片

java - Mapview 顶部的另一个片段(SupportMapFragment)

Android - 能够在没有支持库的情况下使用 AppCompatActivity

android - 两个奇怪的问题

android - 获取Json数组中的数据并发送给服务器

java - android 单选按钮接受

android - 关闭没有动画的抽屉导航

android - 如果我的应用程序的最小 skd = 21,我应该使用哪些支持库?

java - 为什么 ActivityCompat.requestPermissions() 只接受 0-255 之间的整数请求码?