简体   繁体   English

Android 如何制作自定义可绘制形状

[英]Android how to make custom drawable shape

Hi I want to draw a custom shape in xml like below.您好,我想在 xml 中绘制一个自定义形状,如下所示。 How can I draw following shape in android?如何在 android 中绘制以下形状?

如何使用xml在android中绘制咨询费形状

square shape.xml:方形.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffffff"/>

    <stroke android:width="3dp"
        android:color="@color/white"
        />

    <padding android:left="1dp"
        android:top="1dp"
        android:right="1dp"
        android:bottom="1dp"
        />

    <corners android:radius="1dp"
        android:bottomRightRadius="1dp" android:bottomLeftRadius="1dp"
        android:topLeftRadius="1dp" android:topRightRadius="1dp"/>
</shape>

Create a new drawable xml named as svg :创建一个名为 svg 的新可绘制对象svg

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="100dp"
    android:height="100dp"
    android:viewportWidth="100"
    android:viewportHeight="100">
    <path
        android:pathData="M78.2,90L50,61.8 21.8,90V10h56.4v40z"
        android:fillColor="@color/colorPrimary"/>
</vector>

change pathdata to alter the shape as you prefer...更改pathdata以根据需要更改形状...

Use it in your layout xml for example like:在您的布局 xml 中使用它,例如:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/svg">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My text"
        android:layout_centerInParent="true"/>
</RelativeLayout>

You forgot this part android:shape="rectangle" in your root tag you can choose ring oval line and rectangle shape from it set your code like this您在根标签中忘记了这部分 android:shape="rectangle" 您可以从中选择环形椭圆线和矩形形状,这样设置您的代码

 <?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ffffffff"/>

<stroke android:width="3dp"
    android:color="@color/white"
    />

<padding android:left="1dp"
    android:top="1dp"
    android:right="1dp"
    android:bottom="1dp"
    />

<corners android:radius="1dp"
    android:bottomRightRadius="1dp" android:bottomLeftRadius="1dp"
    android:topLeftRadius="1dp" android:topRightRadius="1dp"/>
</shape>
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#ffffffff" />

    <stroke android:width="3dp"
        android:color="@color/white" />

    <padding android:left="1dp"
        android:top="1dp"
        android:right="1dp"
        android:bottom="1dp"
        />

    <corners android:radius="1dp"
        android:bottomRightRadius="1dp" android:bottomLeftRadius="1dp"
        android:topLeftRadius="1dp" android:topRightRadius="1dp"/>
</shape>

You forgot to give android:shape="rectangle" in shape tag.您忘记在 shape 标签中提供 android:shape="rectangle" 。 "line", "oval", "ring" can also be used for respective shape. “线”、“椭圆”、“环”也可以用于相应的形状。 If all the corners are 1dp just use android:radius="1dp".如果所有角都是 1dp,只需使用 android:radius="1dp"。

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

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