【问题标题】:Gravity for xml drawablexml可绘制的重力
【发布时间】:2015-04-12 15:39:16
【问题描述】:

我想创建一个基于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"/>

感谢您的宝贵时间。

【问题讨论】:

  • 你在哪里使用这个drawable?发布它
  • @Rod_Algonquin 你是说布局 xml。我贴出来了。感谢您的宝贵时间。
  • 我仍然想知道图像后面的勾号。因为我想把它放在上面 我们可以用那个功能的背景吗?如果没有我们如何实现这个功能:自定义onDraw功能?

标签: android xml xml-drawable


【解决方案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>

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

一些可能有趣的链接示例:

Set border and background color of textView

Drawable Resource, Android API

how to put a border around an android textview

Tips & Tricks -XML Drawables Part 1

【讨论】:

  • 我明白了你的想法。谢谢你。但是不符合我的要求
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-08
  • 2017-07-17
  • 2013-03-01
  • 2012-01-13
  • 1970-01-01
相关资源
最近更新 更多