【问题标题】:How can I develop this kind of Button我怎样才能开发这种按钮
【发布时间】:2016-10-13 18:06:54
【问题描述】:

此按钮的外层为白色,内层为灰色。
内部背景包含一个图像(勾号)和一个文本(应用)。

我可以做出这样的形状(下图)。

我在 drawable (button.xml) 中使用了一个形状:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="oval">
        <size android:height="85dp"
            android:width="90dp"/>
        <solid android:color="@color/grey"/>
        <stroke android:color="@color/white" android:width="4dp"></stroke>
    </shape>
</item>
<item
    android:drawable="@drawable/checkmark_filled"
    android:bottom="20dp"
    android:left="20dp"
    android:right="20dp"
    android:top="20dp"/>
</layer-list>

并使用 ImageView 来使用形状

<ImageView
    android:id="@+id/button_apply"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/button"/>

但我无法制作形状文本。
我错过了什么还是有什么办法?

【问题讨论】:

  • 题主理解有问题吗?还是无关紧要?
  • @BobMalooga 请再次考虑这个问题。我没有足够的声望。所以无法发布图片。并且不知道它需要登录。
  • i am not able to make a text in shape 文本不是可绘制对象本身的一部分。您可以做的是使用可绘制对象作为 TextView 的背景。并使用 TextView 的文本。
  • @BobMalooga 你能投票赞成这个问题吗?否则我需要等待 2 天才能提出问题 :(
  • 我添加了一个完整的答案,应该可以帮助您通过一个简单的技巧解决您的问题。

标签: java android


【解决方案1】:

我会做这样的事情:

1 - 将您的 LayerList 分成 2 个不同的可绘制对象

circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
        <size android:height="85dp" android:width="90dp" />
        <solid android:color="@color/grey" />
        <stroke android:color="@color/white" android:width="4dp" />
</shape>

我假设你已经有了这个位图:drawable/checkmark_filled

2 - 使用 TextView,而不是 ImageView:

<TextView
    android:id="@+id/button_apply"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgound="@drawable/button"
    android:drawableTop="drawable/checkmark_filled"
    android:text="APPLY"
    android:clickable="true"
/>

根据需要调整重力和填充。
还可以根据需要设置一些其他属性。

请注意,您可以(并且应该)使用字符串资源,而不是硬编码文本。


简要说明:

  • 我使用椭圆形作为 TextView 的背景。
  • 我将复选标记位图用作复合可绘制对象。
  • 我正在使用 TextView 的文本来写东西。
  • 我将TextView设置为clickable,所以你可以把它当作一个Button来使用。

【讨论】:

    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 2017-08-20
    • 2023-02-23
    • 2014-08-13
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多