ConstraintLayout 通过形状资源文件设置圆角不起作用

Cyr*_*rus 5 android android-layout kotlin android-recyclerview android-constraintlayout

我遇到了一个奇怪的问题,当我通过资源文件设置用于适配器项目角的布局时,它不起作用shape

\n\n

这是我的shape资源文件

\n\n
<?xml version="1.0" encoding="utf-8"?>\n\n<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">\n    <corners android:radius="8dp" />\n</shape>\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是我的适配器项目布局

\n\n
<?xml version="1.0" encoding="utf-8"?>\n<layout xmlns:android="http://schemas.android.com/apk/res/android"\n    xmlns:app="http://schemas.android.com/apk/res-auto"\n    xmlns:tools="http://schemas.android.com/tools">\n\n    <data>\n\n        <import type="android.view.View.OnClickListener" />\n\n        <import type="android.view.View" />\n\n        <import type="com.zhixin.wedeep.homepage.data.model.BriefComposition" />\n\n        <variable\n            name="clickListener"\n            type="OnClickListener" />\n\n        <variable\n            name="composition"\n            type="BriefComposition" />\n    </data>\n\n    <androidx.constraintlayout.widget.ConstraintLayout\n        android:layout_width="108dp"\n        android:layout_height="150dp"\n        android:background="@drawable/homepage_shape_recommend_composition_background"\n        android:gravity="center"\n        android:onClick="@{clickListener}"\n        android:orientation="vertical">\n\n        <TextView\n            android:id="@+id/text_view_label_is_new"\n            android:layout_width="36dp"\n            android:layout_height="22dp"\n            android:text="@string/homepage_new"\n            android:textColor="@color/color_white"\n            android:textSize="@dimen/font_size_10"\n            android:visibility=\'@{composition.tag == "NEW" ? View.VISIBLE : View.INVISIBLE}\'\n            app:layout_constraintRight_toRightOf="parent"\n            app:layout_constraintTop_toTopOf="parent"\n            tools:text="\xe6\x96\xb0\xe4\xb8\x8a" />\n\n        <ImageView\n            android:id="@+id/image_view_cover"\n            imageFromUrl="@{composition.cover}"\n            android:layout_width="match_parent"\n            android:layout_height="match_parent"\n            android:scaleType="centerCrop"\n            tools:src="@drawable/gt3_new_bind_logo" />\n\n        <LinearLayout\n            android:id="@+id/linear_layout_text_area"\n            android:layout_width="match_parent"\n            android:layout_height="42dp"\n            android:orientation="vertical"\n            android:paddingStart="5dp"\n            app:layout_constraintBottom_toBottomOf="parent"\n            tools:ignore="RtlSymmetry">\n\n            <TextView\n                android:id="@+id/text_view_name"\n                android:layout_width="wrap_content"\n                android:layout_height="wrap_content"\n                android:maxLines="1"\n                android:text="@{composition.title}"\n                android:textColor="@color/color_white"\n                android:textSize="@dimen/font_size_13"\n                tools:text="\xe5\x8a\xa9\xe7\x9c\xa0\xe8\x84\x91\xe6\xb3\xa2" />\n\n            <TextView\n                android:id="@+id/text_view_duration"\n                android:layout_width="wrap_content"\n                android:layout_height="wrap_content"\n                android:layout_marginTop="1dp"\n                android:text="@{composition.duration}"\n                android:textColor="@color/color_white"\n                android:textSize="@dimen/font_size_10"\n                tools:text="5-30min" />\n        </LinearLayout>\n\n    </androidx.constraintlayout.widget.ConstraintLayout>\n</layout>\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是我的RecylerView用于显示上述项目布局

\n\n
<androidx.recyclerview.widget.RecyclerView\n    android:id="@+id/recycler_view_recommendation"\n    android:layout_width="match_parent"\n    android:layout_height="wrap_content"\n    android:layout_marginTop="20dp"\n    android:layout_marginStart="15dp"\n    android:layout_marginEnd="15dp"\n    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"\n    app:spanCount="3"\n    tools:itemCount="6"\n    tools:listitem="@layout/homepage_item_recommend_composition" />\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是我的RecylerView适配器

\n\n
class RecommendCompositionsAdapter : ListAdapter<BriefComposition, RecommendCompositionsAdapter.ViewHolder>(BriefCompositionDiffCallback()) {\n\n\n    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {\n        return ViewHolder(HomepageItemRecommendCompositionBinding.inflate(\n                LayoutInflater.from(parent.context), parent, false))\n    }\n\n    override fun onBindViewHolder(holder: ViewHolder, position: Int) {\n\n        val briefComposition = getItem(position)\n        briefComposition?.let {\n            holder.apply {\n                itemView.tag = it\n                bind(createOnClickListener(it.id), it)\n            }\n        }\n    }\n\n    private fun createOnClickListener(id: String): View.OnClickListener {\n        return View.OnClickListener {\n            ARouter.getInstance().build(RouterConstant.PATH_AUDIO_PLAYER).withString(AudioPlayerActivity.KEY_ID, id).navigation()\n        }\n    }\n\n    class ViewHolder(private val binding: HomepageItemRecommendCompositionBinding)\n        : RecyclerView.ViewHolder(binding.root) {\n\n        fun bind(listener: View.OnClickListener, item: BriefComposition\n        ) {\n            binding.apply {\n                clickListener = listener\n                composition = item\n                // from View\n\n                executePendingBindings()\n            }\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

对于这个奇怪的问题有什么想法吗?先感谢您!

\n

Rea*_*hed 8

看起来即使您设置了 的背景ConstraintLayout,它也会被 重叠ImageView,因此您没有得到您所拥有的形状ConstraintLayout。我建议使用 aCardView而不是ConstraintLayout. 将可绘制对象设置为您的背景CardView,并将所有内容放入您的内部CardView,就像您为ConstraintLayout.

我希望这有帮助!