我可以在 XML 中绘制矩形吗? [英] Can I draw rectangle in XML?

查看:32
本文介绍了我可以在 XML 中绘制矩形吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我是否可以在 XML 中绘制矩形.我知道如何以编程方式使用 drawRect 方法进行绘制.

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>

您可以在drawable文件夹内新建一个XML文件,并添加上面的代码,然后保存为rectangle.xml.

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

要在布局中使用它,您需要将 android:background 属性设置为新的可绘制形状.我们定义的形状没有任何尺寸,因此将采用布局中定义的 View 的尺寸.

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,您将使用 android:src.这意味着您可以使用矩形作为 ListViews、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.

这篇关于我可以在 XML 中绘制矩形吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆