【问题标题】:Rectangle shape Button with Circular icon on top in AndroidAndroid中顶部带有圆形图标的矩形按钮
【发布时间】:2017-09-01 00:27:14
【问题描述】:

由于我是 Android 新手,我不知道如何在 1 中创建如下所示的按钮:

按钮应该拥有:

  1. 正常背景为紫色,单击时应变为红色。 (达到)
  2. 按钮左上角的圆形图标,按钮中的一半图标和按钮外的一半图标。
  3. 右侧按钮内的箭头图像。(已实现)

有什么办法可以在按钮的左上角实现这个图标。我为每个按钮分别设置了红色和紫色的图标图像。

通过以下实现,我能够达到上图第 2 部分中的结果:

活动 XML:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btn_ETOPUP"
        style="@style/btnServiceTheme"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="10dp"
        android:text="@string/service_etopup"
        android:textAlignment="center" />
    <Button
        android:id="@+id/btn_QuickPay"
        style="@style/btnServiceTheme"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:text="@string/service_QuickPay"
        android:textAlignment="center" />
    <Button
        android:id="@+id/btn_EVouchers"
        style="@style/btnServiceTheme"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="10dp"
        android:text="@string/service_EVouchers"
        android:textAlignment="center" />
</LinearLayout>

样式 XML:

<style name="btnServiceTheme" parent="AppTheme">
<item name="android:textSize">25sp</item>
<item name="android:textAllCaps">false</item>
<item name="android:textColor">@color/white</item>
<item name="android:drawableRight">@mipmap/arrow_right_white</item>
<item name="android:background">@drawable/button_service</item>
<item name="android:layout_marginLeft">20dp</item>
<item name="android:layout_marginRight">20dp</item>
<item name="android:paddingTop">20dp</item>
<item name="android:paddingBottom">20dp</item>
<item name="android:paddingRight">25dp</item>
<item name="android:drawablePadding">-25dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>

选择器 XML:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/purple" android:state_pressed="false" android:state_selected="false" />
    <item android:drawable="@color/red" android:state_pressed="true" />
    <item android:drawable="@color/red" android:state_pressed="false" android:state_selected="true" />
</selector>

提前致谢。

【问题讨论】:

  • 使用FrameLayout就可以实现。

标签: android button icons android-custom-view android-button


【解决方案1】:

尝试使用下面的 XML 作为单独的布局组件:

<FrameLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_gravity="center">

   <ImageView
       android:layout_width="35sp"
       android:layout_height="35sp"
       android:elevation="3dp"
       android:layout_marginLeft="5dp"
       android:scaleType="fitCenter"
       android:src="@drawable/ic_logo_bmw" />
   <Button
       android:id="@+id/btn_get_data"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_margin="10dp"
       android:background="@drawable/rectangular_bg"
       android:text="Quick-Pay"
       android:textColor="@android:color/white" />

</FrameLayout>

使用包含标签在您各自的 XML 布局中使用它。您的输出将是这样的:

【讨论】:

  • 谢谢,但是当点击按钮时如何更改此图标图像?
  • 添加自定义按钮背景并将其设置为您的按钮。为按钮设置一个 ID 并为其添加一个 OnClickListener。按照此链接中的说明更改按钮背景资源 onClick。 Make an Android button change background on click through XML
猜你喜欢
  • 2017-09-11
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
  • 2013-08-25
  • 2012-05-04
相关资源
最近更新 更多