0

我想创建一个自定义视图,以创建一个ImageView如下所示的小部件,其中可绘制源是相机,背景是带有刻度线的矩形:

小部件的预期外观

问题是:

  • 我无法使用右侧的勾号创建边框:勾号被全屏拉伸,而不是停留在右下角。
  • 如果我用android:background.

这是我的xml文件:

当前边框xml可绘制

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="@android:color/transparent"/>
        <stroke
            android:width="2dip"
            android:color="@color/bg_black"/>
        <padding
            android:left="3dp"
            android:right="3dp"
            android:top="3dp"
            android:bottom="3dp"
            />
    </shape>
</item>
<item >
    <layer-list >
        <item>
            <shape
                android:shape="rectangle">
                <stroke
                    android:width="2dp"
                    android:color="@color/border_yellow"/>
                <size
                    android:width="37dp"
                    android:height="37dp"/>
                <padding
                    android:bottom="3dp"
                    android:left="3dp"
                    android:right="3dp"
                    android:top="3dp"/>
            </shape>
        </item>
        <item android:drawable="@drawable/ic_tick"/>
    </layer-list>
</item>
</layer-list>

图像按钮 xml 布局

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/product1"
    android:background="@drawable/toggle_rectangle_check"/>

谢谢你的时间。

4

1 回答 1

1

我不确定这是否是您正在寻找的那种解决方案,但与其拥有一个包含样式的 xml 文件,我相信最好有一个布局文件来定义您的自定义小部件的布局, 像这样

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@drawable/customWidgetBox"
      android:orientation="vertical">

      <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@drawable/customCheckmark"
       android:gravity="bottom|right" />

</LinearLayout>

您需要将 customCheckmark.xml 和 customWidgetBox.xml 添加到可绘制文件夹

customCheckmark.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <item android:drawable="@drawable/check_mark">
        <stroke android:width="3dp" android:color="@color/blue_button_border" />
   </item>
</shape>

customWidgetBox.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <item android:drawable="@drawable/camera">
        <stroke android:width="3dp" android:color="@color/blue_button_border" />
   </item>
</shape>

不确定这段代码是否完全符合您的要求,但这对您开始很有帮助,因为这绝对是实现它的方法!:)

一些可能对示例感兴趣的链接:

设置textView的边框和背景颜色

可绘制资源,Android API

如何在android textview周围放置边框

提示和技巧 -XML Drawables 第 1 部分

于 2015-02-12T03:18:38.423 回答