【问题标题】:How to create an Button with an image, text, and two colours?如何创建一个带有图像、文本和两种颜色的按钮?
【发布时间】:2018-04-16 18:22:48
【问题描述】:

这是我想到的链接。

https://dribbble.com/shots/1407665-Categories/attachments/204971

有没有人知道的流行图书馆可以管理这个,否则我可以走定制路线。

我在我的 XML 中定义了 9 个按钮没问题。

接下来我知道我必须在可绘制文件夹中创建一个 xml 文件,例如“button_shape.xml”。然后我将它添加到我的代码中:

android:background="@drawable/button_shape"

我让我的按钮有一个图像,我假设:

android:drawableTop="@drawable/buttonImage"

我想最后是如何创建一个使底部颜色与文本大小保持一致的形状。同时允许不同的按钮大小。我也可以通过在每个按钮上设置样式并这样定义它们来轻松地交替颜色:

  <style name ="ButtonTheme.Custom1" parent="Base.Widget.AppCompat.Button.Colored">
        <item name="colorPrimary">@android:color/holo_blue_light</item>
        <item name="colorPrimaryDark">@android:color/holo_blue_dark</item>
    </style>

    <style name ="ButtonTheme.Custom2" parent="Base.Widget.AppCompat.Button.Colored">
        <item name="colorPrimary">@android:color/holo_green_light</item>
        <item name="colorPrimaryDark">@android:color/holo_green_dark</item>
    </style>

    <style name ="ButtonTheme.Custom3" parent="Base.Widget.AppCompat.Button.Colored">
        <item name="colorPrimary">@android:color/holo_red_light</item>
        <item name="colorPrimaryDark">@android:color/holo_red_dark</item>
    </style>

    <style name ="ButtonTheme.Custom4" parent="Base.Widget.AppCompat.Button.Colored">
        <item name="colorPrimary">@android:color/holo_orange_light</item>
        <item name="colorPrimaryDark">@android:color/holo_orange_dark</item>
    </style>

    <style name ="ButtonTheme.Custom5" parent="Base.Widget.AppCompat.Button.Colored">
        <item name="colorPrimary">@android:color/holo_blue_bright</item>
        <item name="colorPrimaryDark">@android:color/holo_blue_light</item>
    </style>

到目前为止,我的自定义形状是这个,很接近;但是如何使较深的颜色保持一致的大小,而其他颜色则根据按钮的大小移动?

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:bottom="10dp">
        <shape android:shape="rectangle" >
            <size android:height="10dp" />
            <solid android:color="@color/colorPrimaryDark" />
        </shape>
    </item>

    <item android:top="120dp">
        <shape android:shape="rectangle" >
            <size android:height="120dp" />
            <solid android:color="@color/colorPrimary" />
        </shape>
    </item>
</layer-list>

【问题讨论】:

  • “第二种”颜色只是看起来是黑色textview,不透明度低,使其半透明。似乎没有什么特别的事情发生。
  • 我认为 textview 会导致以某种方式单击出现问题。我不记得到底是什么,但我记得读过它,所以我想我应该避免使用那种方法。我相信这与响应按钮点击或与按钮一起可点击有关。

标签: android xml android-drawable android-button android-shapedrawable


【解决方案1】:

【讨论】:

  • 这实际上达到了我的效果。此外,为 Recycler View 制作布局比尝试构建我正在查看的这些自定义按钮要容易得多。
猜你喜欢
  • 1970-01-01
  • 2012-04-15
  • 2015-01-03
  • 2015-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
相关资源
最近更新 更多