Android-VideoView内置Recyclerview匹配父级宽度并保持纵横比

Android-VideoView内置Recyclerview匹配父级宽度并保持纵横比,android,kotlin,android-recyclerview,android-videoview,Android,Kotlin,Android Recyclerview,Android Videoview,这是布局的一部分: <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/commentMediaContainer" android:layout_width="match_parent" android:layout_height="wrap_content" android

这是布局的一部分:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/commentMediaContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone">

        <ProgressBar
            android:id="@+id/commentMediaLoading"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <VideoView
            android:id="@+id/commentMediaVideo"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
视频与父视频匹配,但高度不正确

如何使VideoView以正确的纵横比显示视频


当我在
commentMediaVideo
上尝试
wrap\u content
而不是
0dp
时,视频根本不显示

尝试使用自定义的
VideoView
和覆盖的
onMeasure()
方法,如下所示 并在布局中使用
MyVideoView

class MyVideoView : VideoView {
    constructor(context: Context?) : super(context) 
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) 
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes)
    

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        setMeasuredDimension(
            getDefaultSize(suggestedMinimumWidth, widthMeasureSpec),
            getDefaultSize(suggestedMinimumHeight, heightMeasureSpec)
        )
    }
}

如果我理解正确,在添加这个类之后,我只需要使用
class MyVideoView : VideoView {
    constructor(context: Context?) : super(context) 
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) 
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes)
    

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        setMeasuredDimension(
            getDefaultSize(suggestedMinimumWidth, widthMeasureSpec),
            getDefaultSize(suggestedMinimumHeight, heightMeasureSpec)
        )
    }
}