我可以画在XML矩形?矩形、XML

2023-09-12 22:10:31 作者:= 予取予夺,倾尽所有=

我是新的Andr​​oid版。 我不知道如果我能在XML绘制矩形。 我知道如何使用drawRect方法编程方式来绘制。

I am new for Android. I wonder if I can draw rectangle in XML. I know how to draw using drawRect method programmatically.

推荐答案

是的,你可以在这里是一个我在早些时候:

Yes you can and here is one I made earlier:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
    <stroke android:width="2dp" android:color="#ff207d94" />
    <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
    <corners android:radius="5dp" />
    <solid android:color="#ffffffff" />
</shape>

您可以创建可绘制文件夹内的一个新的XML文件,并添加上述code,然后将其保存为rectangle.xml。

You can create a new XML file inside the drawable folder, and add the above code, then save it as rectangle.xml.

要布局内使用它,你将设置安卓背景属性设置为新绘制的形状。我们已经定义的形状没有任何的尺寸,并因此将是在布局定义视图的尺寸

To use it inside a layout you would set the android:background attribute to the new drawable shape. The shape we have defined does not have any dimensions, and therefore will take the dimensions of the View that is defined in the layout.

所以把他们放在一起:

<View
    android:id="@+id/myRectangleView"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:background="@drawable/rectangle"/>

最后,你可以设置这个矩形是任何观的背景下,尽管对于ImageViews你可以使用机器人:SRC 。这意味着您可以使用矩形作为背景列表视图,TextViews ...等。

Finally; you can set this rectangle to be the background of any View, although for ImageViews you would use android:src. This means you could use the rectangle as the background for ListViews, TextViews...etc.