【问题标题】:CardView on Android 5.1 - why show rhombus instead of a circle?Android 5.1 上的 CardView - 为什么显示菱形而不是圆形?
【发布时间】:2019-03-29 17:14:31
【问题描述】:

我有一个带有 ImageView 的 CardView:

<android.support.v7.widget.CardView
        android:id="@+id/message_avatar_card"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginEnd="4dp"
        android:layout_marginStart="4dp"
        app:cardCornerRadius="40dp"
        android:innerRadius="0dp"
        android:thicknessRatio="1.9"
        android:background="@color/fui_transparent">

        <ImageView
            android:id="@+id/message_avatar"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/ic_person_black_24dp"
            android:layout_marginEnd="4dp"
            android:scaleType="centerCrop"
            android:background="@color/fui_transparent"/>
    </android.support.v7.widget.CardView>

在 Android 9.0 上一切正常,请看截图

但在 Android 5.1 上我看到的是 rhombus ,而不是 circle

如何解决?

【问题讨论】:

标签: android android-layout imageview cardview


【解决方案1】:

正确的代码

<android.support.v7.widget.CardView
        android:id="@+id/message_avatar_card"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginEnd="4dp"
        android:layout_marginStart="4dp"
        app:cardCornerRadius="20dp"
        android:innerRadius="0dp"
        android:thicknessRatio="1.9"
        android:background="@color/fui_transparent">

        <ImageView
            android:id="@+id/message_avatar"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/ic_person_black_24dp"
            android:layout_marginEnd="4dp"
            android:scaleType="centerCrop"
            android:background="@color/fui_transparent"/>
    </android.support.v7.widget.CardView>

【讨论】:

    【解决方案2】:

    使用 CircleImageView 库:

    implementation 'de.hdodenhof:circleimageview:2.2.0'
    

    在 XML 中:

    <de.hdodenhof.circleimageview.CircleImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/message_avatar"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:src="@drawable/avatar"
            app:civ_border_width="2dp"
            android:layout_gravity="center"
            app:civ_border_color="YOUR_COLOR"/> // if you don't want give any border remove this line
    

    在java中:

    CircleImageView message_avatar = (CircleImageView) findViewById(R.id.message_avatar);
    

    如果您是动态设置图像,请使用 Picasso 库:

    implementation 'com.squareup.picasso:picasso:2.5.2'
    

    在 Java 中

    Picasso.with(context).load(IMAGE_PATH).into(message_avatar);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-15
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      相关资源
      最近更新 更多