2013-05-01 10 views
17

私はAndroid ListView用のカスタムセルを持っています。このカスタムセルは、内部にいくつかのビューがある相対レイアウトです。各セルの間にはスペースがあるので、シャドウの底にセルを追加したいと思います。ListViewの各セルに影を設定するにはどうすればよいですか?

私はグーグルで遊んでいましたが、何も見つかりませんでしたか?

enter image description here

ありがとう:私はこれに似た何かを達成したいです!

+0

私たちは、常にアートワークでそのようなことを行っています。プログラマのための最も簡単な方法は、色、白いパネル、および影を含むあなたが望むサイズに近いものを作ることです。それから、そこに置いてください。別の方法として、カラー・ストリップ、白いパネル、影を別々のアート作品として持ち、それらをまとめてXMLにすることもできます。 – HalR

+0

お返事ありがとうございます! @StephanBranczyk私が求めているのは、行が持つ影効果を作り出す方法です。あなたが添付した画像では、各行の下部に小さな影の効果があることがわかります... – Andres

答えて

37

は、2つの方法の下で行うことができます

  1. 9パッチ画像 - レイヤーリストを使用して9パッチ
  2. を理解するためのlinkをクリックします - 含まれている「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="@android:color/darker_gray" /> 
           <corners android:radius="5dp"/> 
         </shape> 
        </item> 
        <item android:right="1dp" android:left="1dp" android:bottom="2dp"> 
         <shape 
          android:shape="rectangle"> 
           <solid android:color="@android:color/white"/> 
           <corners android:radius="5dp"/> 
         </shape> 
        </item> 
        </layer-list> 
    

その後、あなたのリスト項目のレイアウトに背景として、このレイヤーリストファイルを追加します(すなわち、のLinearLayoutか等。)。

+0

これはまさに私が探していたものです!どうもありがとうございます! – Andres

+0

ありがとう....完璧な答え – nitin

+0

クール、セカンド作品 – antongorodezkiy

2

最も簡単な方法は間違いなく9パッチにシャドウを構築することです。このようなものの例は次のとおりです。

Box 9-Patch

これは9・パッチとして、それが必要以上に大きくなる方法ですが、私は例のために、それを大きくしたかったです。

1

私はまだあなたの正確なシナリオをテストしていませんが、これはあなたがリストビューに透明仕切りを追加する方法を示します。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ListView 
    android:id="@+id/android:list" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="4dip"/> 

</LinearLayout> 

これは影のラインを追加したい場合は、あなたが必要となるものですプログラム的に

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="@android:color/transparent" /> 
    <stroke 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" 
     android:width="match_content" 
     android:color="@color/black" /> 
    <size android:height="1dp" /> 
</shape> 

アルファ層の上にレンダリングされるため、ストロークの色が黒く表示されないことがあります。さらに、これは現在、底辺だけでなく四角形の四辺を描画します。 (私はこれらの2つの問題についてさらに研究しなければならないだろう)。

これをすべてまとめて配線する方法を知りたい場合は、tutorialを参照してください。

2

私が選択した色が無効になっている、あなたのリスト項目レイアウトの背景として、このレイヤリストを適用します....

<?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="@android:color/darker_gray" /> 
       <corners android:radius="5dp"/> 
     </shape> 
    </item> 
    <item android:right="1dp" android:left="1dp" android:bottom="2dp"> 
     <shape 
      android:shape="rectangle"> 
       <solid android:color="@android:color/white"/> 
       <corners android:radius="5dp"/> 
     </shape> 
    </item> 
    </layer-list> 
1

の下から出てピークレイヤリストの四角形を作成し、セルのビュー自身の主な背景を設定そのxmlの色。

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <!--shadow to bottom and right--> 
    <!--by pushing offset from top and left--> 
    <!--then set foreground-color in android:background in xml--> 
    <item 
     android:left="2dp" 
     android:right="0dp" 
     android:top="2dp" 
     android:bottom="0dp"> 
     <shape 
      android:shape="rectangle"> 
      <solid android:color="@android:color/darker_gray" /> 
     </shape> 
    </item> 
</layer-list> 

in your cell.xml: 

<RelativeLayout 
    android:layout_width="match_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/shadow"> 

    <RelativeLayout 
     android:id="@+id/cell_stuff" 
     android:layout_width="match_content" 
     android:layout_height="match_content" 
     android:background="@android:color/holo_orange_light"> 

     <!--cell stuff--> 

    </RelativeLayout> 

</RelativeLayout> 

ORオールインワン

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <!--shadow to bottom and right--> 
    <!--by pushing offset from top and left--> 
    <item 
     android:left="2dp" 
     android:right="0dp" 
     android:top="2dp" 
     android:bottom="0dp"> 
     <shape 
      android:shape="rectangle"> 
      <solid android:color="@android:color/darker_gray" /> 
     </shape> 
    </item> 
    <!--foreground-color to cover over non-shadow--> 
    <!--need to also offset in opposite direction--> 
    <item 
     android:left="0dp" 
     android:right="2dp" 
     android:top="0dp" 
     android:bottom="2dp"> 
     <shape 
      android:shape="rectangle"> 
      <solid android:color="@android:color/holo_orange_light"/> 
     </shape> 
    </item> 
</layer-list> 

in your cell.xml: 

<RelativeLayout 
    android:layout_width="match_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/shadow_allinone"> 

    <!--cell stuff--> 

</RelativeLayout> 

enter image description here

関連する問題