2015-04-14 64 views
0

我要让绘制如下图所示,制作自定义可绘制形状Android中

enter image description here

我能够使矩形和Triange形状在两个不同的XML文件。但我想联合他们来制作像这样的drawable。

+0

你需要一个[(http://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList) – Blackbelt

+0

如果您有它的形象,为什么使用drawable? – Harry

+1

如果我正确理解这样的drawable的需要,你可以尝试9-patch image ..并且只是使矩形区域可扩展。 –

回答

1

使用layer-list,使这个自定形状drawable

/res/drawable/custom_shape.xml:

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

    <!-- Transparent Rectangle --> 
    <item> 
     <shape android:shape="rectangle"> 
      <size 
       android:width="300dp" 
       android:height="60dp" /> 
      <solid android:color="@android:color/transparent" /> 
     </shape> 
    </item> 

    <!-- Colored Rectangle --> 
    <item 
     android:bottom="20dp"> 
     <shape android:shape="rectangle"> 
      <size 
       android:width="300dp" 
       android:height="60dp" /> 
      <solid android:color="#9A8585" /> 
     </shape> 
    </item> 

    <!-- Bottom Triangle --> 
    <item 
     android:left="80dp" 
     android:right="120dp" 
     android:top="0dp" 
     android:bottom="30dp"> 
     <rotate android:fromDegrees="45"> 
      <shape android:shape="rectangle"> 
       <solid android:color="#9A8585" /> 
      </shape> 
     </rotate> 
    </item> 

    <!-- Top Border --> 
    <item 
     android:bottom="75dp"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#EFC1B3" /> 
     </shape> 
    </item> 

</layer-list> 

USE:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/custom_shape"/> 

OUTPUT:

enter image description here

希望这将有助于〜

0

我希望你需要一个Tabular导航器或ViewPager。我的建议是,

1)使用矩形作为您的背景所有情况。 2)创建相同的可绘制的三角形,但使用背景颜色(白色或透明)作为未选定的项目。

3)根据选择状态

0

使用<layer-list/>对于所有项目

4)的三角形视图的手动setVisibility创建具有三角形背景的图。

这里是一个例子。

在项目目录resDrawable文件夹, 使xml文件并将它命名为layerlist.xml并粘贴下面的代码为您的要求。 您还可以在示例中的<item/>标记中添加可绘制的形状,而不是绘制。并使用此xml作为背景ImageView

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <bitmap android:src="@drawable/android_red" 
     android:gravity="center" /> 
    </item> 
    <item android:top="10dp" android:left="10dp"> 
     <bitmap android:src="@drawable/android_green" 
     android:gravity="center" /> 
    </item> 
    <item android:top="20dp" android:left="20dp"> 
     <bitmap android:src="@drawable/android_blue" 
     android:gravity="center" /> 
    </item> 
</layer-list> 

这里是利用<layer-list/>的结果。

enter image description here

我希望它可以帮助你......