[英]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.