如何设置按钮有图像和阴影?

How to set button have image and shadow?

要制作阴影,请制作阴影 xml 文件并像 android:background="@drawable/shadow" 一样使用。 但是放图片也是android:background="@drawable/image

如何在一个按钮中设置按钮阴影和图像?

对不起,我的英语不流利。

1.使用 ImageButton

试试这个你可以使用 ImageButton :- 显示一个带有图像(而不是文本)的按钮,用户可以按下或单击该按钮。默认情况下,ImageButton 看起来像一个普通的 Button,具有在不同按钮状态期间改变颜色的标准按钮背景。

 <ImageButton
  android:id="@+id/btnMain"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:adjustViewBounds="true"
  android:contentDescription="@string/btnMain_description"
  android:elevation="10dp"
  android:background="@android:drawable/dialog_holo_light_fram‌​e"
  android:scaleType="centerInside"
  android:src="@drawable/IMage"
 />

2。用户 CardVirew 或者你可以尝试将你的 button 添加到 card view 中,像这样

编译此依赖项

compile 'com.android.support:cardview-v7:25.3.1'

这样布局

    <android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="10dp">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/disha"
        android:text="@string/app_name" />
</android.support.v7.widget.CardView>

如有任何疑问,请联系我

使用CardView实现:

将支持库添加到dependencies如下:

dependencies {
compile 'com.android.support:cardview-v7:25.2.0'//Add respective version
  }

在布局中使用它

 <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:id="@+id/card_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/user_image"
                app:cardBackgroundColor="@android:color/white"
                android:foreground="?android:attr/selectableItemBackground"
                android:layout_marginLeft="@dimen/activity_horizontal_margin"
                android:layout_marginRight="@dimen/activity_horizontal_margin"
                android:layout_marginTop="@dimen/padding_small"
                android:layout_marginBottom="@dimen/padding_small"
                app:cardCornerRadius="4dp"
                app:cardElevation="10dp" > 

如果有帮助,请告诉我。

在 res/drawable 中创建 button_selector.xml :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <layer-list>
            <item android:right="5dp" android:top="5dp">
                <shape>
                    <corners android:radius="3dp" />
                    <solid android:color="#D6D6D6" />
                </shape>
            </item>
            <item android:bottom="2dp" android:left="2dp">
                <shape>
                    <gradient android:angle="270" 
                        android:endColor="#E2E2E2" android:startColor="#BABABA" />
                    <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>

并且在您的 xml 布局中:

 <ImageButton
            android:src="@mipmap/ic_launcher"
            android:background="@drawable/shadow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:elevation="10dp"/>