【发布时间】:2022-02-06 00:54:12
【问题描述】:
嘿,我正在使用 android 的自定义视图。在我的自定义视图中,我正在使用 view-pager,我想查看 pager 的角半径为 16dp。我成功地做到了,但问题是自定义视图角显示某种半透明颜色。那么我该如何避免这种情况呢?如果我在自定义视图中进行四舍五入,它工作正常,但我不想成为整个视图,因为在自定义视图中我有很多 testView、图像等。我只想在视图寻呼机中做。我附上它的样子。我在图像的所有角落都做了标记。有人可以指导我吗?
GalleryView.kt
class GalleryView(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {
private var binding: GalleryViewBinding = GalleryViewBinding.inflate(LayoutInflater.from(context), this, true)
init{
//.. Initialise code logic.
}
}
gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/galleryContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/galleryPager"
android:layout_width="match_parent"
android:layout_height="224dp"
android:layout_marginBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
// Textview, images etc
</androidx.constraintlayout.widget.ConstraintLayout>
我不是为 viewpager 适配器添加代码,而是添加布局以及如何实现圆角
viewpager_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gallery_pager_item_background">
<ImageView
android:id="@+id/main_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
gallery_pager_item_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:shape="rectangle">
<solid android:color="@color/black" />
<corners android:radius="16dp" />
</shape>
【问题讨论】:
标签: android android-layout android-linearlayout android-canvas android-drawable