2016-10-17 75 views
2

嗨我想绘制两个水平放置的文本视图之间略微弯曲的分隔线。我不想在背景中放置任何类型的图像。我曾尝试使用XML绘制,但我的代码不起作用。如何创建矩形背景,从一侧稍微弯曲

enter image description here

<?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="100dp" 
     android:height="40dp" /> 
    <solid android:color="#ff0000" /> 
</shape> 
</item> 

<item 
android:right="100dp" 
android:left="-180dp" 
android:top="-25dp" 
android:bottom="-42dp"> 
<rotate 
android:fromDegrees="15"> 
<shape android:shape="rectangle"> 
    <solid android:color="#ffffff" /> 
</shape> 
</rotate> 
</item> 

我想设计的图像显示的布局。

+0

检查我编辑的答案 –

+0

@ AdityaVyas-Lakhan谢谢,它为我工作 –

回答

2

在这种情况下,您可以创建VectorDrawable。请注意,在前棒棒糖版本中,无论如何都会生成图像,否则使用srcCompat

你的矢量绘制看起来是这样的:

<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:width="36dp" 
    android:height="24dp" 
    android:viewportWidth="36.0" 
    android:viewportHeight="24.0"> 
<path 
    android:fillColor="#FF0" 
    android:pathData="M0,0 L0,24 L20,24 L36,0 L0,0 Z"/> 

enter image description here

要根据你的需要,你应该自己编辑提供更准确的

更多about vectors
Simple start

+0

谢谢我会试试这个 –

1

您可以创建一个形状像这样

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 

    > 


     <TextView 
      android:id="@+id/banner" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TADAA!!!" 

      android:gravity="center_vertical" 
      android:paddingLeft="5dp" 
      android:textColor="@android:color/black" 
      android:background="@drawable/myshape" 
      /> 



</LinearLayout> 

myshape.xml

<?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="100dp" 
       android:height="40dp" /> 
      <solid android:color="#ff0" /> 
     </shape> 
    </item> 


    <item 
     android:right="100dp" 
     android:left="100dp" 
     android:top="-100dp" 
     android:bottom="-100dp"> 
     <rotate 
      android:fromDegrees="45"> 
      <shape android:shape="rectangle"> 
       <solid android:color="#ffffff" /> 
      </shape> 
     </rotate> 
    </item> 


    <item 
     android:right="-100dp" 
     android:left="100dp" 
     android:top="-100dp" 
     android:bottom="-100dp"> 
     <rotate 
      android:fromDegrees="45"> 
      <shape android:shape="rectangle"> 
       <solid android:color="#ffffff" /> 
      </shape> 
     </rotate> 
    </item> 

</layer-list> 

输出

enter image description here

0

尝试在XML文件中这个矢量绘制

<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:width="24dp" 
    android:height="24dp" 
    android:viewportHeight="99.0" 
    android:viewportWidth="338.0"> 
    <path 
     android:fillColor="#000000" 
     android:pathData="M0,45.5l0,40.5 163.5,0 163.5,0 0,-40.5 0,-40.5 -163.5,0 -163.5,0 0,40.5zM176,10.6c0,0.3 -10.6,16.3 -23.5,35.5l-23.6,34.9 -61.9,0 -62,0 0,-35.5 0,-35.5 85.5,0c47,0 85.5,0.3 85.5,0.6zM322,45.5l0,35.5 -93.5,0 -93.6,0 18.7,-27.8c10.3,-15.2 21.2,-31.2 24.1,-35.5l5.4,-7.7 69.5,0 69.4,0 0,35.5z" 
     android:strokeColor="#00000000" /> 
</vector>