我能在XML中绘制矩形吗?

137

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


1
说XML既代表一切又代表什么都不是,也就是任何东西…… - ShinTakezou
使用XML的目的是什么?drawRect函数在Canvas上工作,通常用于创建自定义视图。 - 0xC0DED00D
我完全不同意@Creator的观点,除非涉及到更复杂的事情,否则我们很少使用Canvas。XML版本使得在整个应用程序中更改特定UI元素的背景变得容易,因为属性在一个位置被定义。 - Graham Smith
@GrahamSmith 我问了他的目的,这样我就可以知道他想用它做什么。你可能很少使用Canvas,但我在开发游戏时使用过很多次。这里没有什么可以同意或不同意的。 - 0xC0DED00D
@creator 对不起,我觉得我误解了评论的语气,以为是“你为什么要费心呢?”。请原谅我的错误。 - Graham Smith
你可以通过创建资源文件轻松绘制矩形。 - Muhammad Yaseen
7个回答

266

可以的,这是我之前制作的一个示例:

<?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。

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

将所有内容组合在一起:

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

最后,您可以将此矩形设置为任何视图的背景,尽管对于ImageViews,您应该使用android:src。这意味着您可以将该矩形用作ListViews、TextViews等的背景。


1
有什么方法可以让我们可以从 Android 布局设置颜色,而不是在代码中设置 <View 的属性? - kobihudson
我认为您无法为其添加id。 - Moses Aprico
如何在程序中编程更改描边颜色? - Zahidul

37

使用形状Drawable创建rectangle.xml,并将其放入Drawable文件夹中...

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
   <solid android:color="@android:color/transparent"/>
   <corners android:radius="12px"/> 
   <stroke  android:width="2dip" android:color="#000000"/>  
</shape>

将其放入ImageView中。

<ImageView 
android:id="@+id/rectimage" 
android:layout_height="150dp" 
android:layout_width="150dp" 
android:src="@drawable/rectangle">
</ImageView>

希望这能帮助你。

24

快速而简单的方法:

<View
    android:id="@+id/colored_bar"
    android:layout_width="48dp"
    android:layout_height="3dp"
    android:background="@color/bar_red" />

8

尝试一下

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_marginTop="5dp"
                    android:layout_height="wrap_content">

                    <View
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:background="#3fe1fa" />

                    <TextView
                        android:textSize="12dp"
                        android:paddingLeft="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:text="1700 Market Street"
                        android:id="@+id/textView8" />
                </TableRow>

输出

图片描述在此


2
请使用这段代码。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:radius="0.1dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />

    <solid android:color="#Efffff" />

    <stroke
        android:width="2dp"
        android:color="#25aaff" />

</shape>

0
首先创建一个矩形向量资源,然后将此代码放入矩形可绘制文件中,并使用此可绘制物(矩形)作为背景,即 android:background="@drawable/baseline_rectangle_24"。
<?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="#C4C4C4" />
        <padding android:left="2dp"
            android:top="2dp"
            android:right="2dp"
            android:bottom="2dp" />
        <corners android:radius="15dp" />
        <solid android:color="#FFFFFF" />
    </shape>

你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

0

在drawable中创建资源文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#3b5998" />
<cornersandroid:radius="15dp"/>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接