繁体   English   中英

Cardview角落背景不透明

[英]Cardview corner background not transparent

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

例

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

这是我的布局文件,包括两个CardViews。 第一个带有白色角落,第二个带有与view_card.xml基本相同的布局但没有白色角落(透明)。

的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>

更新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组件时,我犯了一个错误。 检查这个答案,看看我做错了什么。

在自定义实现中,在上下文可用的位置使用以下代码:

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

编辑:

使用以下代码为Android版本低于Lollipop以避免上述崩溃。

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

几年后,我得出的结论是,在创建复合组件时,我犯了一个根本性的错误。

制作扩展android.support.v7.widget.CardView的复合组件时,夸大的布局xml也不应包含CardView。 基本上你只是将CardView添加到另一个CardView,这将导致我的自定义CardView背后的白色角落。

接受的答案将解决这个问题。 然而,这不是完美的解决方案。

有两种方法可以解决真正的问题:

  1. 复合视图扩展了android.support.v7.widget.CardView,布局xml不包含android.support.v7.widget.CardView,而是包含LinearLayout作为根。 样式必须在课堂上处理。
  2. 复合视图扩展了LinearLayout,布局xml包含android.support.v7.widget.CardView作为根。

我选择了第一个解决方案来解决这个问题。 我希望这可以帮助那些犯同样错误的人。

看起来你正在为你的布局文件创建一个自定义的CardView格式

<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可能正在扩展某些内容(比如LinearLayout),您可能正在该父视图中创建另一个子视图。 因此,只需尝试将卡片布局的直接父级设置为透明使用

的setBackground();

可能这有帮助。

您可以尝试将圆角视图嵌套在另一个视图/布局中。 这里的外部视图可以具有透明背景,因此即使当内部的圆角在角落上留下空间时,由于外部视图具有透明的背景颜色,所以不会看到。 像这样的东西:

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

将新样式添加到styles.xml文件中,类似于:

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

并使用:

<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"/>

结果: 在此输入图像描述

我在类似的问题上有一个较暗的区域/背景,其中弯曲的角落如此截图中所示。 屏幕截图显示角落里有奇怪的深色

我通过将cardElevation设置为0dp来解决。 如果您需要阴影,这可能对您不起作用。

app:cardElevation="0dp"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM