【问题标题】:Imagebutton full image, round cornersImagebutton 完整图像,圆角
【发布时间】:2011-11-07 02:55:56
【问题描述】:

嗯.. 我的 Android 应用程序中有一些 ImageButtons。我希望他们显示完整的图片。我的意思是:我希望 ImageButton 只是图片,你知道吗?

好的...到目前为止,一切都很好...我可以使用属性“背景”来做到这一点。

不过,我也希望 ImageButton 的角是圆角的。

我的布局文件夹中有以下 xml:

    <?xml version="1.0" encoding="UTF-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="#FFFFFFFF"/>
            <corners android:radius="10dip"/>
            <stroke android:width="8dip"/>
        <size android:height="8dip"/>    
        </shape>

当我不希望图片填满整个 ImageButton 时,它非常有效......但是当我尝试使用图片来使用整个 imagebutton 时......它不显示圆角。

有人可以帮帮我吗?

额外问题:另外,我怎样才能在图像按钮周围有一条漂亮的黑线(圆角)?

【问题讨论】:

    标签: android imagebutton


    【解决方案1】:

    我可以建议你使用 ImageView 而不是 ImageButton 并捕捉它的 onClick()。你可以使用这个 xml 文件为你的图像赋予一个精细的黑色圆边轮廓:

    image_bg.xml:(将此xml文件粘贴到drawable文件夹中)

    <?xml version="1.0" encoding="UTF-8" ?> 
      <shape xmlns:android="http://schemas.android.com/apk/res/android">
      <solid android:color="#000000" /> 
      <stroke android:width="3dp" android:color="#776da8" /> 
      <corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" /> 
      <padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp" /> 
      </shape>
    

    您可以使用它(在您添加图像的 xml 文件中),例如:

    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:id="@+id/image" 
               android:src="@drawable/icon"
               android:background="@drawable/image_bg" /> 
    

    【讨论】:

    • 好建议:ImageViewImageButton 更好。谢谢
    【解决方案2】:

    图片按钮有什么用?我目前正在制作音板,我遇到了同样的问题。然后我意识到它不必是“图像按钮”,您也可以像按钮一样使用它。现在我只是使用imageview,然后使用onclick。至于黑线的美感,我不知道,但我的图像有圆角,因为我通过在 new/other/android 图标下创建图标来引入图像。它会自动在每个 diff 大小类别中生成一个。希望对您有所帮助!

    【讨论】:

      【解决方案3】:

      我相信这会对您有所帮助。在绘制新位图时,您可能可以稍微调整一下以添加黑色边框。

      How to make an ImageView with rounded corners?

      然后您可以将 onClick 处理程序附加到图像视图。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多