Android - 在 ImageView 周围设置边框

标签 android android-layout android-emulator android-widget android-imageview

我有一个固定宽度和高度的单元格,设置为 100x100px。在该单元格内,我想显示一个带有边框的 ImageView
我的第一个想法是给ImageView放一个背景资源,并添加1dp的padding来创建边框效果:

<LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/image_border"
        android:padding="1dp"
        android:src="@drawable/test_image" />

</LinearLayout>

显然这应该有效,但它没有,或者至少不像预期的那样。
问题是 ImageView 背景填充了整个 100x100px 单元格的空间,因此,如果图像的宽度小于 100px,则顶部和底部边框会显得更大。

注意黄色边框,我需要它在 ImageView 周围正好 1px:

enter image description here

非常感谢任何帮助、想法、建议。

最佳答案

如果您将 padding=1 和背景颜色放在 LinearLayout 中,您将有一个 1px 的黄色边框。

关于Android - 在 ImageView 周围设置边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12572312/

相关文章:

Android 应用程序适用于 Eclipse 模拟器,但 Play 商店说我的设备不兼容

android - react-native在Android上运行教程 'AwesomeProject'结果 'Unable to download JS Bundle'红屏

android - 使用图形 API 获取 Facebook 页面事件

android - 当我使用 'poweroff' 重新启动时,如何在 HTC 中获取 Boot_Complete Intent ?

android - Android 中的 Reddit 集成

android - 我可以更改图像边框的默认颜色吗?

android - 将 Skobbler map 添加到我的空应用程序

Android 设计支持库 TabLayout 使用自定义选项卡布局但布局包装选项卡

android - 在 ScrollView 中使用表格布局

android - 如何在 Android Emulator 中模拟总网络丢失