如何为imageButton xamarin.android画一个椭圆?

How to draw a ellipse for imageButton xamarin.android?

ImageButton 的默认样式是正方形。

<ImageButton
    android:id="@+id/imgNextPlayer"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:background="@android:color/white"
    android:layout_gravity="top"
    android:tint="#757575" />

您是否尝试过在可绘制文件夹中创建形状 mybuttonshape.xml:

像这样:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding="10dp">
    <solid android:color="#FFFFFF"/> 
    <corners android:radius="10dp"/>
</shape>

然后将按钮的背景设置为此可绘制对象:

<ImageButton
    android:id="@+id/imgNextPlayer"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:background="@drawable/mybuttonshape"
    android:layout_gravity="top"
    android:bottomRightRadius="10dp"
    android:bottomLeftRadius="10dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp" />

检查此 question 其他选项

您可以将 android:background="@android:.... 设置为可绘制对象:

android:background="@drawable/myellipsebutton"

然后添加一个名为 myellipsebutton.xml:

的可绘制对象
  • Resource/Drawable/myellipsebutton.xml

在此drawable resource中,您可以定义按钮的形状,在本例中我使用oval而不是rectanglelinering 也可用:


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="oval">
            <gradient android:startColor="#F44336" android:endColor="#E57373" android:angle="270" />
            <stroke android:width="10dp" android:color="#607D8B" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="oval">
            <gradient android:startColor="#3F51B5" android:endColor="#9FA8DA" android:angle="270" />
            <stroke android:width="10dp" android:color="#607D8B" />
        </shape>
    </item>
    <item>
        <shape android:shape="oval">
            <gradient android:startColor="#2196F3" android:endColor="#BBDEFB" android:angle="270" />
            <stroke android:width="1dp" android:color="#607D8B" />
        </shape>
    </item>
</selector>