Android - MapFragment覆盖按钮阴影,就像MyLocation按钮一样

时间:2015-10-16 10:48:25

标签: java android material-design mapfragment mylocationoverlay

我正在努力为Google地图上的自定义叠加按钮添加阴影。我的目标是让我的自定义按钮看起来就像Google的MyLocation按钮。这是我的应用程序现在的样子:

enter image description here

layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    style="@style/AppTheme">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true" />


    <ImageButton
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/ic_search_black_24dp"
        android:tint="@color/icon_gray"
        android:onClick="searchButtonClicked"
        android:background="@drawable/mapbutton"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <com.quinny898.library.persistentsearch.SearchBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/searchbox"
            />

    </LinearLayout>

</RelativeLayout>

mapbutton.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false">
        <shape android:shape="rectangle">
            <corners android:radius="1.3dp" />
            <solid android:color="#55FFFFFF" />
        </shape>
    </item>
    <item
        android:state_pressed="true"
        android:state_enabled="true">
        <shape android:shape="rectangle">
            <corners android:radius="1.3dp" />
            <solid android:color="#ccb3b3b3" />
        </shape>
    </item>
    <item
        android:state_focused="true"
        android:state_enabled="true">
        <shape android:shape="rectangle">
            <corners android:radius="1.3dp" />
            <solid android:color="#A9FFFFFF" />
        </shape>
    </item>
    <item android:state_enabled="true">
        <shape android:shape="rectangle">
            <corners android:radius="1.3dp" />
            <solid android:color="#baffffff" />
        </shape>
    </item>
</selector>

我已经尝试更改ImageButton上的android:elevation值。不幸的是,添加高程值似乎不会为按钮添加阴影。有没有办法为我的mapbutton drawable添加阴影?

2 个答案:

答案 0 :(得分:0)

试试此代码

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item android:left="5dp" android:top="5dp">
<shape>
<corners android:radius="3dp"/>
<solid android:color="#D6D6D6"/>
</shape>
</item>
<item android:bottom="2dp" android:right="2dp">
<shape>
<gradient android:angle="270" android:endColor="#DC5F0A" android:startColor="#E68F54"/>
<!--
 <stroke android:width="1dp" android:color="#BABABA" /> 
-->
<corners android:radius="4dp"/>
<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"/>
</shape>
</item>
</layer-list>
</item>
</selector>

我希望它会。

答案 1 :(得分:0)

来自here对我有用的是:

<ImageButton
  android:clipToPadding="false"
  android:outlineProvider="bounds"
  android:elevation="2dp" />