我想知道我是否可以在XML中画矩形。我知道如何以编程方式使用drawRect方法进行绘图。
发布于 2012-04-12 22:00:55
是的,你可以,这是我之前做的:
<?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文件,并添加上述代码,然后将其保存为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...etc的背景。
发布于 2014-08-22 20:43:31
快速和肮脏的方式:
<View
android:id="@+id/colored_bar"
android:layout_width="48dp"
android:layout_height="3dp"
android:background="@color/bar_red" />
发布于 2016-07-21 14:25:31
尝尝这个
<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>
输出
https://stackoverflow.com/questions/10124919
复制相似问题