Android - 围绕ImageView设置边框

And*_*Res 29 android android-widget android-emulator android-layout android-imageview

我有一个固定宽度和高度的单元格,让它为100x100px.在那个单元格中,我想要显示一个ImageView带边框的边框.
我的第一个想法是将背景资源放入ImageView,并添加1dp的填充以创建边框效果:

<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>
Run Code Online (Sandbox Code Playgroud)

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

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

在此输入图像描述

任何帮助,想法,任何建议都非常感谢.

Chr*_*ine 46

如果你将填充= 1和背景颜色放在LinearLayout中,你将有1px黄色边框.


Ray*_*ter 26

这对我有用...

<!-- @drawable/image_border -->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="@color/colorPrimary"/>
  <stroke android:width="1dp" android:color="#000000"/>
  <padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp"/>
</shape>

<ImageView
  android:layout_width="300dp"
  android:layout_height="300dp"
  android:layout_gravity="center"
  android:padding="1dp"
  android:cropToPadding="true"
  android:scaleType="centerCrop"
  android:background="@drawable/image_border"/>
Run Code Online (Sandbox Code Playgroud)

这是我使用带有边框的viewpager和imageview获得的结果.

带有边框1dp黑色的示例imageview.

  • 我想念android:cropToPadding ="true".感谢现在为我工作. (6认同)
  • 很好的充电器:) (3认同)

Pau*_*bra 9

如果图像的大小可变,那么您将始终获得该效果.我想你需要为ImageView设置一个固定的大小,并给它一个设置的背景颜色 - 从你的例子黑色的外观是有意义的.将imageview包装在FrameLayout中,或仅包含黄色背景和1px填充的视图.

编辑


我想到了这个,我的回答感觉不对......

如果您为每个ImageView设置固定大小,填充和边距.然后根据需要设置背景颜色,您可以获得所需的效果.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" >


        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#52D017"
            android:padding="1dp"
            android:layout_margin="5dp"
            android:src="@drawable/test1"
            tools:ignore="ContentDescription" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:background="#52D017"
            android:padding="1dp"
            android:src="@drawable/test2"
            tools:ignore="ContentDescription" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在屏幕截图中,两个显示的图像的宽度小于100px,高度不同.

例

这不处理具有透明背景的图像,因此(在这种情况下)黄绿色显示通过.您可以通过将每个ImageView包装在FrameLayout中来解决此问题.使ImageView背景变黑并使用所需的填充将FrameLayout设置为WrapContent(我认为)