简体   繁体   English

在Android中制作自定义可绘制形状

[英]Make Custom Drawable Shape in Android

I want to make drawable like below, 我想像下面这样绘制

在此处输入图片说明

I am able to make the Rectangle and Triange shape in two different xml files. 我可以在两个不同的xml文件中制作Rectangle和Triange形状。 But i want to joint them to make the drawable like this. 但我想与他们合作使像这样的可绘制对象。

Use <layer-list/> for that. 为此使用<layer-list/>

Here is an example for instance. 这是一个例子。

in Drawable folder of res in project directory, make xml file and name it as layerlist.xml and paste it below code as your requirement. 在项目目录中res Drawable文件夹中,制作xml文件并将其命名为layerlist.xml ,然后根据layerlist.xml其粘贴到代码下方。 you can also add your shape drawable instead of drawable in the <item/> tag in the example. 您还可以在示例中的<item/>标记中添加图形可绘制而不是可绘制。 and use this xml as a background of ImageView . 并将此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> 

Here is the result of the use of <layer-list/> . 这是使用<layer-list/>

在此处输入图片说明

I hope it helps you... 希望对您有帮助...

Use layer-list to make this custom shape drawable . 使用layer-list使此自定义形状drawable

/res/drawable/custom_shape.xml: /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: 输出:

在此处输入图片说明

Hope this will help~ 希望这会有所帮助〜

I expect that you need it for aa Tabular navigator or a ViewPager . 我希望您需要Tabular导航器或ViewPager My suggestion is that 我的建议是

1)Use the rectangle as your background for all the cases. 1)在所有情况下都使用矩形作为背景。

2)Create the same triangle drawable but with the background color (White or transparent) for the unselected item. 2)为未选择的项目创建相同的三角形可绘制对象,但具有背景色(白色或透明)。

3)Create a view with the triangle background for all the items 3)为所有项目创建一个带有三角形背景的视图

4)Manually setVisibility of the triangle view according to the selection state 4)根据选择状态手动​​设置三角形视图的setVisibility

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM