【问题标题】:Android: Drawing a circle inside a buttonAndroid:在按钮内画一个圆圈
【发布时间】:2021-10-04 12:47:51
【问题描述】:

我需要在按钮内创建一个圆圈,如下图所示:

但我的问题是我已经在使用按钮的背景属性通过以下方式添加漂亮的阴影效果: android:background="@android:drawable/dialog_holo_light_frame"

所以我想我不能同时使用背景。

这是我的按钮当前的外观:

知道如何同时获得同样漂亮的阴影效果和里面的圆圈吗?也许可以使用“样式”属性? (不知道)

【问题讨论】:

  • 您是否尝试过使用框架布局组合或使用 CardView(用于阴影效果和所有)创建按钮,然后在其中添加视图。
  • 感谢您的回复@Basu。有没有可能举个例子?
  • "Test" 并且数量应该是 @Basu 建议的布局或卡片视图中的 2 个视图
  • 下面有一个答案,你可以试试。

标签: android background geometry styles drawable


【解决方案1】:

你可以像这样使用 CardView:

<com.google.android.material.card.MaterialCardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        app:cardCornerRadius="5sp"
        app:cardElevation="10dp"
        app:strokeColor="@color/black"
        app:strokeWidth="1dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="10dp"
                android:gravity="center"
                android:text="Test"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <Button
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/round_button"
                android:gravity="center_vertical|center_horizontal"
                android:text="1"
                android:textColor="#fff" />
        </LinearLayout>

</com.google.android.material.card.MaterialCardView>

round_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="oval">
            <solid android:color="#fa09ad"/>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="#c20586"/>
        </shape>
    </item>
</selector>

【讨论】:

  • 非常感谢@javdromero。你的例子很完美!
猜你喜欢
  • 2016-10-02
  • 2019-05-11
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多