将按钮的外观更改为圆形按钮

change the appearance of button into round button

我想把现在的长方形按钮做成圆形,在里面加上分享按钮的图片。我的意思是按钮应该是圆形的,它应该只有 "share"

的图像(适合)
<Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_marginTop="8dp"
        android:text="03"
        android:textStyle="bold"/>

您可以创建一个单独的可绘制对象并实现圆形。然后将 drawable 设置为按钮的背景,如下所示

将可绘制文件创建为 circle_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid
        android:color="#008577"/>

    <size
        android:width="120dp"
        android:height="120dp"/>
</shape>

然后更改布局如下

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/circle_button"
   android:text="Button"/>

你可以找到答案Here

创建资源可绘制对象

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  <stroke android:width="1dp" android:color="#FF404040" /> 
  <corners android:radius="6dp" /> 
  <gradient android:startColor="#FF6800" android:centerColor="#FF8000" android:endColor="#FF9700" android:angle="90" /> 
</shape>

然后设置为按钮背景。

android:background="@drawable/button_background"

将可绘制文件创建为 bg_share.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid
    android:color="@color/colorPrimaryDark">
</solid>

<corners
    android:radius="500dp">
</corners>

<padding
    android:top="0dp"
    android:right="10dp"
    android:left="10dp"
    android:bottom="0dp">
</padding>

</shape>

并在您的 main.xml 中创建 imageview 而不是类似按钮

 <ImageView
       android:id="@+id/share"                         
       android:layout_width="wrap_content"                        
       android:layout_height="wrap_content"                        
       android:background="@drawable/bcshare"
       android:padding="10dp"
       android:src="@drawable/ic_share_black_24dp" />

在MainActivity.java中做这样的事情

 share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           //Your code...
        }
    });

在 drawable 文件夹中创建一个 xml 并将其命名为:"round.xml" 并在给定的按钮背景中使用此 xml。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle">
    <solid android:color="#eeffffff" />
    <corners android:bottomRightRadius="8dp"
        android:bottomLeftRadius="8dp"  
        android:topRightRadius="8dp"
        android:topLeftRadius="8dp"/>
</shape>

对于布局中的按钮 xml 您可以使用最后一行设置背景

<Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_marginTop="8dp"
        android:text="03"
        android:textStyle="bold"
        android:background="@drawable/round"/>

您可以使用带有 Widget.MaterialComponents.Button.Icon 样式的标准 MaterialButton

类似于:

   <com.google.android.material.button.MaterialButton
            android:layout_width="48dp"
            android:layout_height="48dp"
            style="@style/Widget.MaterialComponents.Button.Icon"
            app:icon="@drawable/ic_share"
            app:iconSize="24dp"
            app:iconPadding="0dp"
            android:insetLeft="0dp"
            android:insetTop="0dp"
            android:insetRight="0dp"
            android:insetBottom="0dp"
            app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.Button.Rounded"
            />

使用 app:shapeAppearanceOverlay 获得圆角。这样你就有了一个圈子。

  <style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">32dp</item>
  </style>