【问题标题】:How to set custom view corner color transparent in android如何在android中设置自定义视图角颜色透明
【发布时间】: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


    【解决方案1】:

    显示的角可能来自构成 ViewPagerViewGroup。由于您尚未发布所有代码,因此很难知道。

    罪魁祸首可能是Decor View。在主要活动中尝试以下操作,看看边缘的颜色是否发生变化。

    getWindow().getDecorView().setBackground(new ColorDrawable(getResources()
            .getColor(android.R.color.holo_green_light)));
    

    如果这不起作用,请查看其他 ViewGroup 父级以查看颜色进入的位置。您可以使用布局检查器查看视图层次结构。

    问题也可能在于您构建自定义视图的方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 2012-01-20
      • 1970-01-01
      • 2019-10-21
      • 2017-11-02
      相关资源
      最近更新 更多