首页 > 解决方案 > ImageButton 透明背景的阴影

问题描述

我有一个图像按钮。当我添加透明背景时,阴影消失了。如何在没有第三方库的情况下修复它?

                <ImageButton
                android:id="@+id/player_activity_previous_button"
                android:background="@android:color/transparent"
                android:tint="@color/color_for_buttons"
                android:src="@drawable/ic_skip_previous_48"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:layout_toLeftOf="@id/player_activity_play_pause_button"
                android:layout_centerVertical="true"
                android:stateListAnimator="@null"
                android:elevation="4dp"
                android:translationZ="4dp"/>

标签: androidxmlandroid-layout

解决方案


您可以使用 cardView 和 imageView 的组合而不是具有透明背景的 imageButton,首先但在 gradle 文件中添加一个依赖项。

dependencies {
compile 'com.android.support:cardview-v7:23.0.1'
-------

}

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="8dp"
card_view:cardBackgroundColor="@android:color/white">

<ImageView
    android:id="@+id/dish_image"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:adjustViewBounds="true" />

增加阴影效果只需增加卡片高度


推荐阅读