【问题标题】:Add shadow behind the image view in android [duplicate]在android中的图像视图后面添加阴影[重复]
【发布时间】:2017-02-14 06:18:06
【问题描述】:

在android中的image view后面添加阴影 这是 android 中带有默认阴影的示例图像:

和xml:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.home.fmat.MainActivity">

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/on"
    android:src="@drawable/Google_Duo"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"/>

</RelativeLayout>

【问题讨论】:

  • 显示 xml 布局
  • 你可以设置空背景,但我认为android:background="?android:attr/selectableItemBackground"更正确
  • 那个问题没有问到改变背景颜色

标签: android


【解决方案1】:

该背景是默认的 ImageButton 背景。 将ImageButton 更改为ImageView

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.home.fmat.MainActivity">

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/on"
    android:background="@drawable/bg_button"
    android:src="@drawable/Google_Duo"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"/>

</RelativeLayout>

或者如果你不想点击效果消失,设置ImageButton背景为自定义背景,例如使用statelistdrawable,例如在你的drawables中,你可以定义bg_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/red" android:state_pressed="true"/>
    <item android:drawable="@android:color/red" android:state_hovered="true"/>
    <item android:drawable="@android:color/transparent"/>    
</selector>

【讨论】:

  • statelistdrawable的使用方法
  • 我更新了我的答案,希望对你有帮助
  • 在创建 bg_button.xml 时出现错误“必须声明元素选择器”
  • 请确保将bg_button.xml 放在文件夹drawable 中,而不是放在layout 文件夹中
  • 哦,对不起,我把它放在了值中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-25
  • 1970-01-01
  • 2016-03-20
  • 2020-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多