Android 为imageview圈出背景

Android 为imageview圈出背景,android,android-imageview,android-drawable,android-image,android-shape,Android,Android Imageview,Android Drawable,Android Image,Android Shape,我试图放置三个圆形背景的图像视图。我还画了一张圆形的画 但背景不是圆形,而是椭圆形 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:orientation="horizontal"> <Im

我试图放置三个圆形背景的图像视图。我还画了一张圆形的画

但背景不是圆形,而是椭圆形

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/contactphone"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:src="@drawable/phonecall"
            android:layout_margin="10dp"
            android:background="@drawable/contact_icon_round"
            android:layout_height="75dp" />

        <ImageView
            android:id="@+id/contactemail"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:src="@drawable/mail"
            android:layout_margin="10dp"
            android:background="@drawable/contact_icon_round"
            android:layout_height="75dp" />

        <ImageView
            android:id="@+id/contactlocation"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:src="@drawable/location"
            android:layout_margin="10dp"
            android:background="@drawable/contact_icon_round"
            android:layout_height="75dp" />
            android:layout_gravity="fill_vertical" />
    </LinearLayout>

android:layout\u gravity=“fill\u vertical”/>
圆形可拉伸:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:color="@color/white"/>
    <size android:width="5dp" android:height="5dp"/>
</shape>

有什么不对劲,但却不能找出错误


谢谢

只要您的图像源是一个完美的正方形,您就可以将所有
image
s的
android:layout\u height
属性更改为
wrap\u content
。 这样,您仍然可以使用
权重
行为


否则,您必须使用固定且相等的宽度和高度,并删除重量属性…

使用此布局

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="15dp"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >

  <LinearLayout
      android:layout_width="0dp"
      android:layout_weight="1"
      android:layout_height="75dp"
      >
  <ImageView
      android:id="@+id/contactphone"
      android:layout_width="75dp"
      android:src="@drawable/phonecall"
      android:layout_margin="10dp"
      android:background="@drawable/contact_icon_round"
      android:layout_height="75dp" />
  </LinearLayout>
  <LinearLayout
      android:layout_width="0dp"
      android:layout_weight="1"
      android:layout_height="75dp"
      >
  <ImageView
      android:id="@+id/contactemail"
      android:layout_width="75dp"
      android:src="@drawable/mail"
      android:layout_margin="10dp"
      android:background="@drawable/contact_icon_round"
      android:layout_height="75dp" />
  </LinearLayout>
  <LinearLayout
      android:layout_width="0dp"
      android:layout_weight="1"
      android:layout_height="75dp"
      >
  <ImageView
      android:id="@+id/contactlocation"
      android:layout_width="75dp"
      android:src="@drawable/location"
      android:layout_margin="10dp"
      android:background="@drawable/contact_icon_round"
      android:layout_height="75dp" />
  </LinearLayout>
</LinearLayout>

由于宽度和高度不均匀,背景显示为椭圆形


希望它能对您有所帮助。

在没有XML的圆形图像视图后面绘制一个带2像素黑色边框的圆形黄色背景:


为您的图像视图提供精确的宽度和高度,而不是包裹内容此解决方案的可能副本是可以的,但不会在Android设备的屏幕密度范围内(xhdpi->xxxhdpi)进行一致的缩放。是的,您可以通过使用相应大小的值来解决这个问题,但这需要尝试一个错误以使它们看起来一致。
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.YELLOW);          // (no gradient)
gd.setStroke(2, Color.BLACK);
gd.setShape(GradientDrawable.OVAL);
gd.setGradientType(GradientDrawable.RADIAL_GRADIENT);
gd.setGradientRadius(iv.getWidth()/2);
iv.setBackground(gd);