Android 我可以在 XML 中绘制矩形吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10124919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:34:58  来源:igfitidea点击:

Can I draw rectangle in XML?

androidxmldraw

提问by user1301568

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

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

回答by Graham Smith

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 文件,并添加上面的代码,然后将其保存为 rectangle.xml。

To use it inside a layout you would set the android:backgroundattribute 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...等的背景。

回答by N.Droid

Create rectangle.xmlusing Shape Drawable Like this put in to your DrawableFolder...

rectangle.xml使用 Shape Drawable创建像这样放入您的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>

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.

希望这会帮助你。

回答by Pimentoso

Quick and dirty way:

快速而肮脏的方式:

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

回答by A.G.THAMAYS

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

输出

enter image description here

在此处输入图片说明

回答by Faxriddin Abdullayev

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>

回答by Muhammad Kamran

create resource file in drawable

在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"/>