我能用XML画矩形吗?

时间:2023-02-09 23:55:30

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

我想知道我是否可以用XML表示矩形。我知道如何以编程的方式绘制drawRect方法。

5 个解决方案

#1


174  

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>

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

您可以在drawable文件夹中创建一个新的XML文件,并添加上面的代码,然后将其保存为rectangley . 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.

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

So putting it all together:

所以把它们放在一起

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

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.

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

#2


29  

Create rectangle.xml using Shape Drawable Like this put in to your Drawable Folder...

创建矩形。xml使用像这样的形状可绘制文件放入您的可绘制文件夹……

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

put it in to an ImageView

把它放到一个ImageView中。

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

Hope this will help you.

希望这能对你有所帮助。

#3


16  

Quick and dirty way:

快速和肮脏的方式:

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

#4


7  

try this

试试这个

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

output

输出

我能用XML画矩形吗?

#5


1  

Use this code

使用这个代码

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

#1


174  

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>

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

您可以在drawable文件夹中创建一个新的XML文件,并添加上面的代码,然后将其保存为rectangley . 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.

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

So putting it all together:

所以把它们放在一起

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

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.

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

#2


29  

Create rectangle.xml using Shape Drawable Like this put in to your Drawable Folder...

创建矩形。xml使用像这样的形状可绘制文件放入您的可绘制文件夹……

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

put it in to an ImageView

把它放到一个ImageView中。

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

Hope this will help you.

希望这能对你有所帮助。

#3


16  

Quick and dirty way:

快速和肮脏的方式:

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

#4


7  

try this

试试这个

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

output

输出

我能用XML画矩形吗?

#5


1  

Use this code

使用这个代码

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

相关文章