【问题标题】:How to create small round buttons that contains an icon at the centre?如何创建在中心包含图标的小圆形按钮?
【发布时间】:2018-01-02 08:01:31
【问题描述】:

我想在我的 android 应用程序中创建按钮,如下图所示

您可以看到下面的所有四个按钮。

我想给他们一个自定义颜色背景,然后在顶部添加一个图标。我无法调整按钮中心的图标。

怎样才能达到想要的效果?

【问题讨论】:

  • 这里是最好的按钮库 - android-arsenal.com/details/1/6285
  • Android 库就足够了。只需阅读this answer
  • @ThomasMary 我知道如何创建圆形按钮,但我无法在按钮中心放置带有彩色背景的图标。

标签: android


【解决方案1】:

您可以使用 FrameLayout 执行此操作,对于圆形背景,您需要创建一个可绘制形状的文件。请参考我的答案。

<FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/round_shape"
        android:clickable="true"
        android:focusable="true"
        android:visibility="visible"
        android:foreground="?android:attr/selectableItemBackground"
        android:padding="4dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_star_border_black_24dp"/>
    </FrameLayout>

round_shape.xml将此文件添加到drawable文件夹中

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <gradient
        android:angle="270"
        android:startColor="@color/profile_grey"
        android:endColor="@color/profile_grey"/>

</shape>

【讨论】:

    【解决方案2】:

    创建可绘制对象并将该可绘制对象用作按钮背景。

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item>
        <shape android:shape="oval">
            <stroke android:color="#e1e1e1"
                android:width="0.5dp"/>
            <solid android:color="@android:color/white"/>
        </shape>
      </item>
    </selector>
    

    此代码创建一个白色背景的圆圈。

    【讨论】:

    • 确实如此,但是在背景顶部放置一个图标并且在中间也放置一个图标是另一回事。
    • @YatendraRathore,看看我的回答,它可能对你有帮助
    • &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/rounded_button" android:src="@drawable/img" android:padding="10dp"/&gt; 试试这个。使用图像视图而不是按钮。
    猜你喜欢
    • 2018-09-23
    • 2019-12-24
    • 2015-07-27
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    相关资源
    最近更新 更多