不知道为什么它不起作用。给出here的答案不适用于我,因为我的尺寸已经在dp
。
这是我的活动
hits_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:algolia="http://schemas.android.com/apk/res-auto">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/user_image"
android:layout_width="100dp"
android:layout_height="100dp"
app:civ_border_width="4dp"
android:layout_marginStart="8dp"
android:layout_marginTop="20dp"
app:civ_border_color="#c42f92"
android:scaleType="fitCenter"
algolia:attribute='@{"image"}'/>
<TextView
android:id="@+id/user_name"
android:paddingTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
algolia:attribute='@{"username"}'
algolia:highlighted='@{true}'/>
</LinearLayout>
</layout>
此行会使您的应用崩溃
android:scaleType="fitCenter"
来自CircleImageView github。
限制
ScaleType始终是CENTER_CROP,如果您尝试更改它,您将获得异常。这是(目前)设计,因为它对于个人资料图像非常好。
在logcat中,您将看到此错误。
Caused by: java.lang.IllegalArgumentException: ScaleType FIT_CENTER not supported.
解决方案:从布局xml文件中删除android:scaleType="fitCenter"
。
这是因为你错误地将标签添加为View xml。这是错的:
<!-- Below line is in a wrong place and wrong tag. -->
<layout xmlns:algolia="http://schemas.android.com/apk/res-auto">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
</LinearLayout>
它应该是:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:algolia="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</LinearLayout>