【问题标题】:Can I draw rectangle in XML?我可以在 XML 中绘制矩形吗?
【发布时间】:2012-04-24 21:04:55
【问题描述】:

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

【问题讨论】:

  • 说 XML 意味着所有和没有,即任何东西......
  • 使用 XML 的目的是什么? drawRect 在 Canvas 上工作,通常用于创建自定义视图。
  • 我完全同意@Creator,我们很少使用 Canvas,除非它用于更复杂的东西。由于属性被定义在一个位置,XML 版本可以轻松更改整个应用程序中特定 UI 元素的背景。
  • @GrahamSmith 我问了这个目的,这样我就可以知道他想用这个做什么。你可能很少使用 Canvas,我在开发游戏时使用过很多次。这里没有什么可以同意或不同意的。
  • @creator 对不起,我想我把评论的语气误解为“你为什么要打扰?”。我很抱歉。

标签: android xml draw


【解决方案1】:

是的,你可以,这是我之前做的一个:

<?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...等的背景。

【讨论】:

  • 有人会如何制作它,以便可以从我们制作
  • 我认为你不能为它添加 id
  • 如何以编程方式更改笔触颜色?
【解决方案2】:

使用 Shape 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>

希望这会对你有所帮助。

【讨论】:

    【解决方案3】:

    快速而肮脏的方式:

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

    【讨论】:

      【解决方案4】:

      试试这个

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

      输出

      【讨论】:

        【解决方案5】:

        使用此代码

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

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-06-29
            • 2016-10-12
            • 1970-01-01
            • 2013-03-25
            相关资源
            最近更新 更多