我想创建一个带有圆角的复合视图,看起来像这样:
在Android中如何实现这一点?
如果有人遇到相同的问题,最好的方法是使用9.png图像。在xml中不可能实现。
<vector
来实现这一点,在这里是invert_shape.xml
:<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportWidth="400"
android:viewportHeight="400">
<path
android:pathData="M3.146,256.500 L3.146,73.449 C41.770,72.388 72.757,41.041 72.757,2.500 L271.500,2.500 L271.500,256.500 L3.146,256.500 Z"
android:fillColor="#eeffcc"
android:strokeColor="#000000"
android:strokeWidth="5"/>
</vector>
接下来,您需要将此 drawable
设置为您的 activity_main.xml
中 LinearLayout
的 android:background
,如下所示:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linear_layout"
android:background="@drawable/invert_shape"
android:layout_centerInParent="true"
android:orientation="horizontal" />
结果:
您可能会想知道如何在<vector
中实现android:path
,我使用了一个简单的解决方案。您可以使用像Photoshop
或Illustrator
这样的程序,在其中创建您的形状layer
。完成后,右键单击layer
并选择“Copy SVG
”。现在,您已经复制了该形状,请将其粘贴到drawable
(此处为invert_shape.xml
)中的Android Studio
中,您将获得来自您的Photoshop形状的android:path
和android:stroke
参数。去掉不必要的参数,并确保在XML
本身中使用正确的属性。现在,您的创意没有尽头。干杯! :)您可以通过将以下框架设置为背景来为布局提供单侧角落。
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dip"
/>
<solid
android:color="#3D2A1D"/>
<corners
android:radius="20sp"
android:topRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"/>
</shape>
我不确定负边框半径。