2016-09-30 10 views
1

rectangle shapeAndroidの長方形

どのように私は影で2つの異なる色を使用することにより、矩形の形状を作成することができますか?上記の画像のように。

+1

使用「」 – SripadRaj

答えて

3
Please create a drawable file and put the below code in it. 

    <?xml version="1.0" encoding="utf-8"?> 
     <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
      <item> 
       <shape android:shape="rectangle" > 
        <size android:height="20dp" /> 
        <solid android:color="#F66D08" /> 
       </shape> 
      </item> 
      <item android:top="50dp"> 
       <shape android:shape="rectangle" > 
        <gradient android:endColor="#AD1B1D" 
         android:startColor="#E2360A" 
         android:angle="270" /> 
       </shape> 
      </item> 
     </layer-list> 
1

enter image description here置き、あなたのactivity_mainでこれらのコード:

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

    <LinearLayout 
     android:gravity="center" 
     android:background="#F66D08" 
     android:layout_width="match_parent" 
     android:layout_height="20dp"> 

    </LinearLayout> 

    <LinearLayout 
     android:background="@drawable/introslidergradiant" 
     android:layout_width="match_parent" 
     android:layout_height="100dp"></LinearLayout> 
</LinearLayout> 

そして作成drawble RESがintroslidergradiant.xml名前のファイル:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape> 
      <gradient 
       android:angle="90" 
       android:startColor="#A31720" 
       android:endColor="#E1350B" 
       android:type="linear" /> 
     </shape> 
    </item> 
</selector> 
0
<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape"> 
    <stroke android:width="2dp" android:color="#ff207d94" /> 
    <padding android:left="2dp" 
     android:top="2dp" 
     android:right="2dp" 
     android:bottom="2dp" /> 
    <corners android:radius="5dp" /> 
    <solid android:color="#ffffffff" /> 
</shape> 

あなたは内部の新しいXMLファイルを作成することができますdrawableフォルダを作成し、上記のコードを追加して、rectangle.xmlとして保存します。

レイアウト内で使用するには、android:background属性を新しい描画可能な形状に設定します。私たちが定義したシェイプにはディメンションがないため、レイアウトで定義されているViewのディメンションが使用されます。

だから一緒にそれをすべて置く:最後に

<View 
    android:id="@+id/myRectangleView" 
    android:layout_width="200dp" 
    android:layout_height="50dp" 
    android:background="@drawable/rectangle"/> 

。この矩形を任意のビューの背景に設定することができますが、ImageViewの場合はandroid:srcを使用します。つまり、ListView、TextViewsなどの背景として長方形を使用できます。

0

あなたが描画可能

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
<gradient 
android:type="linear" 
android:startColor="#FFFF820D" 
android:endColor="#FFA32815" 
android:angle="90"/> 
</shape> 

次のコードを使用するか、またはあなただけの色を設定し、アンドロイドのオプションを選択して、それはあなたのためにグラデーションを生成します独自のグラデーションhereを生成することができますのXMLファイルを作成する必要が。

5

層-リストは、この

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

    <item> 
     <shape android:shape="rectangle"> 
      <size 
       android:width="40dp" 
       android:height="40dp" /> 
      <solid android:color="#F86F05" /> 
     </shape> 
    </item> 

    <item android:top="10dp"> 
     <shape android:shape="rectangle"> 
      <size 
       android:width="30dp" 
       android:height="30dp" /> 
      <solid android:color="#B31F19" /> 
     </shape> 
    </item> 

</layer-list> 

そして同じのスクリーンショットを解決するために使用することができます。

enter image description here

あなたの代わりに固体の色のグラデーションをしたい場合は、startColor、endColorと角度を勾配設定する固形変更。

+2

完璧なようです。良いアプローチのために 'gradient'を追加してください –