繁体   English   中英

Android CardView透明背景

[英]Android CardView transparent background

我想创建一个卡片视图,其中卡片上有一张简单的照片,右上角有一张照片可以将其删除。 因此,为了使底部完全可见,我需要使卡片比照片大,但我不想显示它。 所以我尝试将背景设置为透明,并且在设计时一切都很好

设计时间

这是设计时android studio的屏幕截图:
A)显示卡的外观
B)是同一张卡,当我单击它时,表明该卡比照片本身大一点(当选择一个元素时,蓝色边框是正常的android studio边框)

但是在运行时,卡的高度有些奇怪:

运行

代码如下:

impactr_card.xml

   <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardView"
        android:layout_width="160dp"
        android:layout_height="160dp"
        android:layout_gravity="center"
        android:layout_marginBottom="@dimen/smallMargin"
        android:elevation="0dp"
        card_view:cardBackgroundColor="@android:color/transparent"
        card_view:cardCornerRadius="0dp">


        <ImageView
            android:layout_width="@dimen/influencerCardSize"
            android:layout_height="@dimen/influencerCardSize"
            android:layout_gravity="bottom"
            android:scaleType="centerCrop"
             />

        <TextView
            android:layout_width="@dimen/influencerCardSize"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:alpha="0.7"

            android:background="@color/colorPrimaryDark"
            android:gravity="center"
         />

        <ImageButton

            android:layout_width="@dimen/removeButtonSize"
            android:layout_height="@dimen/removeButtonSize"
            android:layout_gravity="right|top"
            android:background="?android:attr/selectableItemBackground"
            android:src="@drawable/ic_remove" />

    </android.support.v7.widget.CardView>

new_influencers.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">



    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="@dimen/influencersViewHeight"
        android:layout_gravity="center"

        android:layout_margin="@dimen/smallMargin"
        android:background="@android:color/transparent"
        android:elevation="0dp"
        android:scrollbars="vertical"
        android:visibility="visible" />




</LinearLayout>

我不更改Java代码上与样式相关的任何内容...

我怎样才能消除这种烦人的海拔

想要使卡片变得不可见,使“ alpha”属性1

如果要在CardView删除高程,

使用app:cardElevation = "0dp"代替android:cardElevation="0dp"

使用card_view:cardElevation="0dp"代替android:elevation="0dp" 希望这对您有帮助

 <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardView"
        android:layout_width="160dp"
        android:layout_height="160dp"
        android:layout_below="@+id/linearLayout"
        android:layout_centerHorizontal="true"
        card_view:cardElevation="0dp"
        card_view:cardBackgroundColor="@android:color/transparent">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="10dp"
            android:src="@drawable/img"
            android:scaleType="centerCrop"
            android:layout_marginRight="10dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:alpha="0.7"
            android:text="text"
            android:layout_marginEnd="10dp"
            android:background="@color/colorPrimaryDark"
            android:gravity="center"
            android:layout_marginRight="10dp" />

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="right|top"
            android:background="?android:attr/selectableItemBackground"
            android:src="@drawable/img" />

    </android.support.v7.widget.CardView>

如果您不希望高程,则应考虑不使用CardView,因为CardView默认会带来高程。

取而代之的是,将CardView替换为ConstraintLayout或RelativeLayout之类的容器。 这些选项默认情况下不带有任何高程,它们是透明的,并且如果您愿意,还可以向它们添加前景纹波效果。

暂无
暂无

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

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