2017-08-01 84 views
4

要製作陰影,請製作陰影xml文件並使用像android:background="@drawable/shadow"。 但是放圖像也是android:background="@drawable/image如何設置按鈕有圖像和陰影?

如何在一個按鈕中設置按鈕陰影和圖像?

對不起,我不會說流利的英語。

+0

採取的ImageButton –

+0

可以卡視圖中添加按鈕,以獲得更好陰影效果 – darwin

+0

我發現了一個代碼使陰影。但它必須使用'android:background'選項。但把圖像放在按鈕中也是使用'android:background'。我想知道如何在同一時間使用... –

回答

2

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 或者你可以試試這個添加buttoncard 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> 

問我任何查詢

+0

,圖像沒有完全填滿。如何解決這個問題? –

+0

@ DongHyeongJeong檢查更新ans用戶android:layout_height =「wrap_content」 –

+0

哦..我犯了一個錯誤..!謝謝 –

0

使用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" > 

如果有幫助,請現在讓我。

0

在res 創建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"/>