How to set the button have image and shadow?

To make a shadow, make a shadow xml file and use as android:background="@drawable/shadow". But also put an image android:background="@drawable/image.

How to set the shadow and button image in one button?

I'm sorry that I do not speak English.

+4
source share
3 answers

1. use ImageButton

try this, you can use ImageButton: - displays a button with an image (instead of text) that can be clicked or clicked by the user. By default, ImageButton looks like a regular button with a standard button background that changes color in different button states.

 <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>

+2

CardView :

lib 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" > 

, , .

+1

create button_selector.xml in res / drawable:

<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>

And in your xml layout:

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

Source: https://habr.com/ru/post/1682717/


All Articles