2014-06-07 623 views
82

我想創建這樣的LinearLayout作爲例子相同的邊框:的Android的LinearLayout:陰影添加邊框周圍的LinearLayout

enter image description here

在這個例子中,我們可以看到邊框是不一樣的圍繞線性佈局。 我怎樣才能創建這個使用XML可繪製文件?

現在,我只能夠創建一個簡單的邊框四周像這樣的LinearLayout:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <corners 
     android:radius="1dp" 
     android:topRightRadius="0dp" 
     android:bottomRightRadius="0dp" 
     android:bottomLeftRadius="0dp" /> 
    <stroke 
     android:width="1dp" 
     android:color="#E3E3E1" /> 

    <solid android:color="@color/blanc" /> 

</shape> 
+0

您可以使用'background'屬性...創建一個形狀類似矩形,顏色和陰影效果的XML文件,並將其設置爲您的線性佈局的背景。 –

+0

使用描邊爲灰色邊框和填充效果 –

+0

我認爲,它的兩個佈局xml,比如說一個是線性佈局,內部是相對佈局,有填充 –

回答

157

試試這個..

<?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="#CABBBBBB"/> 
      <corners android:radius="2dp" /> 
     </shape> 
    </item> 

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

@Hariharan非常感謝!我嘗試了你的代碼,它似乎確定。但是,還有一件事要做,我怎樣才能在linearlayout的左側和右側添加一個薄邊框(1dp)? (作爲例子) – wawanopoulos

+2

@wawanopoulos在Hariharan的回答中用'「1dp」替換''0dp''。 –

+0

您可以將#EAEAEA顏色添加到父級佈局中,以便像問題圖片 –

16

作爲替代方案,你可以使用9補丁影像爲背景,爲您的佈局,讓更多的「自然」的影子:

enter image description here

結果:

enter image description here

把圖像中的/res/drawable文件夾中。
確保文件擴展名是.9.png,不.png

順便說一句,這是在API 19 SDK資源文件夾中的現有資源的修改(降低到最小平方大小)。
我留下了紅色標記,因爲它們看起來並不有害,如draw9patch工具所示。

[編輯]

關於9修補程序,如果你從未有過的事情與他們無關。

只需將其添加爲您的視圖的背景。

黑色標記的區域(左和頂部)將伸展(垂直,水平)。
黑色標記的區域(右側,底部)定義了「內容區域」(可以添加文本或視圖的位置 - 如果需要,可以調用未標記的區域「填充」)。

教程:http://radleymarx.com/blog/simple-guide-to-9-patch/

+1

@Der Golem我從來不使用9patch,它的工作原理是什麼? – wawanopoulos

+1

只需將其添加爲視圖的背景。黑色標記的區域(左側和頂部)將伸展(垂直,水平)。黑色標記區域(右側,底部)定義了「內容區域」(如果需要,可以添加文本或視圖 - 將其稱爲「填充」)。教程:http://radleymarx.com/blog/simple-guide-to-9-patch/ –

26

好吧,我知道這是太晚。但我有相同的要求。我解決了這樣的問題

1.首先在「drawable」 文件夾中創建一個xml文件(例如:border_shadow.xml),並將下面的代碼複製到其中。

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

<item> 
    <shape> 
     <!-- set the shadow color here --> 
     <stroke 
      android:width="2dp" 
      android:color="#7000" /> 

     <!-- setting the thickness of shadow (positive value will give shadow on that side) --> 

     <padding 
      android:bottom="2dp" 
      android:left="2dp" 
      android:right="-1dp" 
      android:top="-1dp" /> 

     <corners android:radius="3dp" /> 
    </shape> 
</item> 

<!-- Background --> 

<item> 
    <shape> 
     <solid android:color="#fff" /> 
     <corners android:radius="3dp" /> 
    </shape> 
</item> 

2.now你想上哪兒陰影(例如:LinearLayout中)佈局Android中添加此:背景

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="8dip" 
    android:background="@drawable/border_shadow" 
    android:orientation="vertical"> 

併爲我工作。

45

我通過使用9補丁圖形獲得最佳效果。

可以簡單地通過使用下列編輯器創建一個單獨的9貼片圖形: http://inloop.github.io/shadow4android/

實施例:

的9貼片圖形:

The 9 patch graphic:

其結果是:

enter image description here

來源:

<LinearLayout 
android:layout_width="200dp" 
android:layout_height="200dp" 
android:orientation="vertical" 
android:background="@drawable/my_nine_patch" 
+0

如何隱藏黑線?請幫忙 – Isaac

+0

它顯示黑線 – Prabs

+0

@Isaac將圖像保存爲my_nine_patch.9.png(.9.png是9個批處理圖像) – arun

42

這就是爲什麼CardView存在。 CardView | Android Developers
這只是一個FrameLayout,支持預棒棒糖設備中的高程。

<android.support.v7.widget.CardView 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:cardUseCompatPadding="true" 
    app:cardElevation="4dp" 
    app:cardCornerRadius="3dp" > 

    <!-- put whatever you want --> 

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

要使用這個,你需要添加依賴於build.gradle

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

如何更改陰影顏色 –

+1

有史以來最好的解決方案。 – CodeToLife

+0

陰影很弱https://stackoverflow.com/questions/49076188/cardview-shadow-for-imageview – user924

15

這是如此簡單:影子

與這樣的梯度創建一個可繪製文件在視圖下方below_shadow.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<gradient 
    android:startColor="#20000000" 
    android:endColor="@android:color/transparent" 
    android:angle="270" > 
</gradient> 
</shape> 

的陰影視圖above_shadow.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<gradient 
    android:startColor="#20000000" 
    android:endColor="@android:color/transparent" 
    android:angle="90" > 
</gradient> 
</shape> 

等爲左,右陰影只是改變梯度:)

+0

我在哪裏調用below_shadow?一個視圖的背景? –

+1

創建below_shadow xml文件,然後在您的視圖中,在您的視圖中添加一個「視圖」,並設置below_shadow.xml作爲此視圖的背景 –

+0

我做了同樣的事,但我的「above_shadow」不透明,在自定義視圖中查看。它工作正常單獨。我該怎麼辦? – SoYoung

-2

您可以用9補丁圖形做到這一點的角度之上,但它是錯誤的方式,因爲你必須使用PNG文件。我認爲你應該使用xml文件(可繪製文件)。 嘗試使用此代碼

activity_main.xml 

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:padding="10dp" 
android:orientation="vertical"> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="3dp" 
    android:layout_marginLeft="3dp" 
    android:layout_marginRight="3dp" 
    android:background="@drawable/gradient_top"/> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 
    <LinearLayout 
     android:layout_width="3dp" 
     android:layout_height="match_parent" 
     android:layout_marginBottom="3dp" 
     android:background="@drawable/gradient_left"/> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginRight="3dp" 
      android:layout_marginBottom="3dp" 
      android:background="@color/md_white_1000" 
      android:orientation="vertical"> 

     </LinearLayout> 

    <LinearLayout 
     android:layout_width="3dp" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="-4dp" 
     android:layout_marginBottom="3dp" 
     android:background="@drawable/gradient_right"/> 
</LinearLayout> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="3dp" 
    android:layout_marginTop="-4dp" 
    android:layout_marginLeft="3dp" 
    android:layout_marginRight="3dp" 
    android:background="@drawable/gradient_bottom"/> 

gradient_top.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<gradient 
    android:startColor="#dadada" 
    android:endColor="#f2f2f2" 
    android:angle="90"/> 
</shape> 

gradient_left.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<gradient 
    android:startColor="#dadada" 
    android:endColor="#f2f2f2" 
    android:angle="180"/> 
</shape> 

gradient_right.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<gradient 
    android:startColor="#dadada" 
    android:endColor="#f2f2f2" 
    android:angle="0"/> 
</shape> 

gradient_bottom.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<gradient 
    android:startColor="#dadada" 
    android:endColor="#f2f2f2" 
    android:angle="270"/> 
</shape> 

enter image description here

+8

如此醜陋的解決方案...太多的嵌套LinearLayouts。 – Lester

5

您在名稱drop_shadow繪製創建一個文件.XML。XML:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!--<item android:state_pressed="true"> 
     <layer-list> 
      <item android:left="4dp" android:top="4dp"> 
       <shape> 
        <solid android:color="#35000000" /> 
        <corners android:radius="2dp"/> 
       </shape> 
      </item> 
      ... 
     </layer-list> 
    </item>--> 
    <item> 
     <layer-list> 
      <!-- SHADOW LAYER --> 
      <!--<item android:top="4dp" android:left="4dp"> 
       <shape> 
        <solid android:color="#35000000" /> 
        <corners android:radius="2dp" /> 
       </shape> 
      </item>--> 
      <!-- SHADOW LAYER --> 
      <item> 
       <shape> 
        <solid android:color="#35000000" /> 
        <corners android:radius="2dp" /> 
       </shape> 
      </item> 
      <!-- CONTENT LAYER --> 
      <item android:bottom="3dp" android:left="1dp" android:right="3dp" android:top="1dp"> 
       <shape> 
        <solid android:color="#ffffff" /> 
        <corners android:radius="1dp" /> 
       </shape> 
      </item> 
     </layer-list> 
    </item> 
</selector> 

然後:

<LinearLayout 
... 
android:background="@drawable/drop_shadow"/> 
0

雅馬赫迪AJ ---爲RelativeLayout的

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle"> 
      <gradient 
       android:startColor="#7d000000" 
       android:endColor="@android:color/transparent" 
       android:angle="90" > 
      </gradient> 
      <corners android:radius="2dp" /> 
     </shape> 
    </item> 

    <item 
     android:left="0dp" 
     android:right="3dp" 
     android:top="0dp" 
     android:bottom="3dp"> 
     <shape android:shape="rectangle"> 
      <padding 
       android:bottom="40dp" 
       android:top="40dp" 
       android:right="10dp" 
       android:left="10dp" 
       > 
      </padding> 
      <solid android:color="@color/Whitetransparent"/> 
      <corners android:radius="2dp" /> 
     </shape> 
    </item> 
</layer-list> 
0

我發現來解決這個的最佳方式。

  1. 您需要在佈局上設置一個堅實的矩形背景。

  2. 使用此代碼 - ViewCompat.setElevation(view , value)

  3. 在父佈局設置android:clipToPadding="false"

+1

如果您不知道,這是ViewCompat類的'ViewCompat.setElevation()'方法的當前實現:'public void setElevation(View view,float elevation){}'。正如你所看到的,它沒有實現。我真的懷疑它會有什麼幫助,並且真的懷疑你是否真的測試了自己的答案,哈哈 – mradzinski