2014-01-19 59 views
70

我周圍搜索,我找不到一個正確的方法來做到這一點。我想對我的看法如下陰影效果: enter image description hereAndroid查看影子

enter image description here

說實話,我不知道這第二個是通過將陰影效果來完成。有任何想法嗎?

+1

你的意思是? HTTP://計算器。com/questions/4406524 /如何設置陰影在Android中查看(檢查最高票的答案,而不是標記的答案) –

+1

@DatVM,謝謝,這似乎是要做的伎倆,但我想也許有android sdk中的工具內置。例如,通過添加一些代碼行來爲線性佈局添加陰影:P – longwalker

回答

130

我知道這個問題已經回答了,但我希望你知道,我發現了一個drawableAndroid Studio這是非常相似的,你在這個問題有圖片: 看看這個:

android:background="@drawable/abc_menu_dropdown_panel_holo_light" 

它看起來像這樣:

enter image description here

希望這將有助於

上述

編輯

的選項是舊版本的Android Studio,所以你可能不會發現它。對於較新的版本:

android:background="@android:drawable/dialog_holo_light_frame" 

而且,如果你想擁有自己的自定義形狀,我建議使用繪圖軟件,如Photoshop和繪製。

enter image description here

不要忘了將它保存爲.9.png文件(例如:my_background.9.png

閱讀文檔:Draw 9-patch

編輯2

一個更美好,更小辛勤工作的解決方案是使用CardView並設置app:cardPreventCornerOverlap="false"以防止v IEWS重疊邊界:

<android.support.v7.widget.CardView 
    android:id="@+id/card_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:cardCornerRadius="2dp" 
    app:cardElevation="2dp" 
    app:cardPreventCornerOverlap="false" 
    app:contentPadding="0dp"> 

    <!-- your layout stuff here --> 

</android.support.v7.widget.CardView> 

還要確保已列入build.gradle的最新版本,目前是

compile 'com.android.support:cardview-v7:26.0.0' 
+0

謝謝,實際上,它看起來非常相似。我還爲Android發現了「google card layout」,它們非常酷! – longwalker

+6

太棒了。只是未來觀衆的參考,這是'/ /extras/android/support'。 – theblang

+0

abc_menu_dropdown_panel_holo_light沒有列出我.. –

86

我使用過Android Studio 0.8.6,我找不到:

android:background="@drawable/abc_menu_dropdown_panel_holo_light" 

所以我發現這個:

android:background="@android:drawable/dialog_holo_light_frame" 

,它看起來像這樣:

enter image description here

28

創建card_background。XML在res /可繪製文件夾下面的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 

<item> 
    <shape android:shape="rectangle"> 
     <solid android:color="#BDBDBD"/> 
     <corners android:radius="5dp"/> 
    </shape> 
</item> 

<item 
    android:left="0dp" 
    android:right="0dp" 
    android:top="0dp" 
    android:bottom="2dp"> 
    <shape android:shape="rectangle"> 
     <solid android:color="#ffffff"/> 
     <corners android:radius="5dp"/> 
    </shape> 
</item> 
</layer-list> 

然後將下面的代碼添加到您想要的卡片佈局

android:background="@drawable/card_background" 

以下行定義了陰影的顏色,以該元素該卡

<solid android:color="#BDBDBD"/> 
+3

這不是一個真正的影子 - 因爲它不會褪色。它更像是兩邊的邊界。 –

10

這可能會遲到,但對於那些誰仍在尋找答案爲了這個,我發現git的樞紐一期工程,這是實際適合我的需要唯一的一個。 android-materialshadowninepatch

只需添加您的build.gradle依賴

compile 'com.h6ah4i.android.materialshadowninepatch:materialshadowninepatch:0.6.3' 

歡呼聲此行。爲創造者豎起大拇指! happycodings

+0

這是一個不錯的圖書館! – box

21

CardView給你true在Android 5+的陰影,它有一個支持庫。只需用它包裝你的視圖,你就完成了。

<android.support.v7.widget.CardView> 
    <MyLayout> 
</android.support.v7.widget.CardView> 

它需要下一個依賴關係。

dependencies { 
    ... 
    compile 'com.android.support:cardview-v7:21.0.+' 
} 
+0

這是唯一正確的答案,因爲只有'CardView'對於不同高度的compat模式也有不同的陰影級別。 –

19

推杆@android:drawable/dialog_holo_light_frame背景,給人的影子,但你不能改變背景顏色,也沒有邊框樣式,所以最好從它的陰影中獲益,同時還能夠通過層列表把一個背景

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!--the shadow comes from here--> 
    <item 
     android:bottom="0dp" 
     android:drawable="@android:drawable/dialog_holo_light_frame" 
     android:left="0dp" 
     android:right="0dp" 
     android:top="0dp"> 

    </item> 

    <item 
     android:bottom="0dp" 
     android:left="0dp" 
     android:right="0dp" 
     android:top="0dp"> 
     <!--whatever you want in the background, here i preferred solid white --> 
     <shape android:shape="rectangle"> 
      <solid android:color="@android:color/white" /> 

     </shape> 
    </item> 
</layer-list> 

將它保存在繪製文件夾下的發言權shadow.xml

將其分配到一個視圖,在XML佈局文件中設置它的背景

android:background="@drawable/shadow" 
+0

你檢查過它是否有效嗎?第二個沒有做任何事情。我無法將其更改爲透明或圓角。 – zeeshan

+2

這就是我需要的! –

+1

在我嘗試過的所有東西中,這是唯一可行的,謝謝。 –

0

使用陰影高程屬性影響:

<YourView 
    ... 
    android:elevation="3dp"/> 
+0

使用高程要求設備運行棒棒糖。 –

0

如果您需要正確應用陰影,那麼您必須執行以下操作。

考慮這個視圖中,與背景繪製定義:

<TextView 
    android:id="@+id/myview" 
    ... 
    android:elevation="2dp" 
    android:background="@drawable/myrect" /> 

背景繪製被定義爲具有圓角的矩形:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
    <solid android:color="#42000000" /> 
    <corners android:radius="5dp" /> 
</shape> 

這是appying陰影的recomended方式看看這個https://developer.android.com/training/material/shadows-clipping.html#Shadows