【问题标题】:how to create a circular imageButton in android? [duplicate]如何在android中创建一个圆形imageButton? [复制]
【发布时间】:2015-01-21 12:31:06
【问题描述】:

伙计们,我在我的布局文件中创建了一个 imageButton,并将一个圆形 png 图像设置为它的背景。但是当我运行我的应用程序时,它会向我显示一个方形按钮,我的给定图像放在它的中间。

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/sliderr" />

【问题讨论】:

  • 你可以通过代码做到这一点。

标签: android android-layout


【解决方案1】:

添加android:background="@null"

【讨论】:

  • 这个解决方案很好也很简单,但是如果你想给你的 ImageButton 添加波纹效果,这可能不是最好的解决方案,因为波纹效果仍然会贯穿整个正方形,而不仅仅是图片。
  • android:background="?selectableItemBackgroundBorderless" android:padding="10dp" 添加这将创建带有波纹的圆形图像按钮
  • @ViniciusQueiroz 你是对的。答案比 Android SDK 中的涟漪还要老:)
【解决方案2】:

res/drawable/round_button.xml中创建一个新的形状XML:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <gradient 
        android:startColor="#FFFF0000" 
        android:endColor="#80FF00FF"
        android:angle="270" />
</shape>

使用这个形状作为按钮的背景:

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="@drawable/round_button"
    android:src="@drawable/sliderr" />

您可能还想将android:scaleType="fitCenter" 添加到您的按钮以使图像与按钮的大小相同,如果您的图像具有不相等的高度/宽度,则添加android:adjustViewBounds="true"

【讨论】:

    【解决方案3】:

    尝试添加这个 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="8dip"
        android:bottomLeftRadius="8dip"  
        android:topRightRadius="8dip"
        android:topLeftRadius="8dip"/>
    </shape>
    

    【讨论】:

      猜你喜欢
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      相关资源
      最近更新 更多