视频播放器的布局如下:

video_view.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.google.android.exoplayer2.ui.AspectRatioFrameLayout
        android:id="@+id/aspectRatioLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center">

        <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ImageView
            android:id="@+id/posterImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/poster_image_description" />

        <com.google.android.exoplayer2.ui.SubtitleView
            android:id="@+id/subtitleView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <com.google.android.exoplayer2.ui.PlayerControlView
            android:id="@+id/nativeControls"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </com.google.android.exoplayer2.ui.PlayerControlView>

        <com.sdk.adapters.controls.ControlBar
            android:id="@+id/controls"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

        </com.sdk.adapters.controls.ControlBar>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/messageOverlay"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="#80000000">
                <TextView
                    android:id="@+id/messageOverlayText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:text="@string/message_overlay"
                    android:textColor="@android:color/white"
                    android:textSize="30sp" />
            </FrameLayout>
        </FrameLayout>
    </com.google.android.exoplayer2.ui.AspectRatioFrameLayout>
</FrameLayout>


ControlBar定义为:

control_bar_view.xml

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#80000000">

    <!-- BEGIN: Strut -->
    <View
        android:id="@+id/exitPlaceholder"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.1" />
    <!-- END: Strut -->

    <TextView
        android:id="@+id/broadcastName"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="@string/broadcast_name"
        android:textColor="#ffffff"
        android:textSize="18sp"
        app:layout_constraintEnd_toStartOf="@id/castButton"
        app:layout_constraintStart_toEndOf="@id/exitPlaceholder"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/siteName"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="@string/site_name"
        android:textColor="#ffffff"
        android:textSize="18sp"
        app:layout_constraintEnd_toStartOf="@id/castButton"
        app:layout_constraintStart_toEndOf="@id/exitPlaceholder"
        app:layout_constraintTop_toBottomOf="@id/broadcastName" />

    <ImageButton
        android:id="@+id/castButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:contentDescription="@string/send_to_google_chromecast"
        android:visibility="invisible"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toStartOf="@id/airplayButton"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/airplayButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:contentDescription="@string/send_to_apple_airplay"
        android:visibility="invisible"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toStartOf="@id/optionsButton"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/optionsButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/fullscreen"
        android:scaleType="fitStart"
        android:src="@drawable/closed_caption_gray"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/skipBackButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/skip_back_button"
        android:scaleType="fitStart"
        android:src="@drawable/back_30"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toStartOf="@id/playPauseToggle"
        app:layout_constraintHeight_percent="0.20"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/playPauseToggle"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/play_button"
        android:scaleType="fitStart"
        android:src="@drawable/play"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toStartOf="@id/skipForwardButton"
        app:layout_constraintHeight_percent="0.25"
        app:layout_constraintStart_toEndOf="@id/skipBackButton"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/skipForwardButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/skip_forward_button"
        android:scaleType="fitStart"
        android:src="@drawable/forward_30"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.20"
        app:layout_constraintStart_toEndOf="@id/playPauseToggle"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/currentTime"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:text="@string/null_time"
        android:textColor="#ffffff"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@id/seekBar"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/remainingTime"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:text="@string/null_time"
        android:textColor="#ffffff"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@id/seekBar"
        app:layout_constraintEnd_toEndOf="parent" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:progress="50"
        android:progressDrawable="@drawable/seek_bar"
        android:thumb="@drawable/seek_thumb"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/fullscreenButton"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/fullscreenButton"
        app:layout_constraintVertical_bias="0.0" />

    <ImageButton
        android:id="@+id/fullscreenButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/fullscreen"
        android:scaleType="fitStart"
        android:src="@drawable/fullscreen"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.1" />


</androidx.constraintlayout.widget.ConstraintLayout>



在Android Studio的布局编辑器中,这看起来不错:

java - 为什么我的布局在横向模式下无法填充屏幕?-LMLPHP

但是,当我运行实际的应用程序时,ControlBar(仅ControlBar)没有填充屏幕!其他所有内容都可以完美地填满屏幕:

java - 为什么我的布局在横向模式下无法填充屏幕?-LMLPHP

我究竟做错了什么?

如果有帮助,ControlBar是扩展ConstraintLayout的自定义视图。初始化代码如下所示:

ControlBar.java

public class ControlBar extends ConstraintLayout
{
    private BFPlayer    player;
    private TextView    broadcastName;
    // ... references for the various subviews ...
    private ImageButton fullscreenButton;

    public ControlBar ( Context context )
    {
        super(context);
        init(context);
    }

    public ControlBar ( Context context, AttributeSet attrs )
    {
        super(context, attrs);
        init(context);
    }

    public ControlBar ( Context context, AttributeSet attrs, int defStyleAttr )
    {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init ( Context context )
    {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View           view     = inflater.inflate(R.layout.bf_control_bar_view, null);
        addView(view);

        broadcastName = findViewById(R.id.broadcastName);
        // ... grab references to the various buttons ...
        fullscreenButton = findViewById(R.id.fullscreenButton);

        // ... set up listeners for the various buttons ...
    }

    // ... The rest of the class ...
}


我究竟做错了什么?我敢肯定这很简单,但是我凝视了片刻,无法解决。 (注意:我是Android开发的新手,并且经验不足。我的工作是经常更换平台和编程语言)

最佳答案

当您为View充气时,您没有告诉它父级是什么,因此它不能正确应用LayoutParams:

View view = inflater.inflate(R.layout.bf_control_bar_view, null);
addView(view);


相反,告诉它直接向父级充气:

View view = inflater.inflate(R.layout.bf_control_bar_view, this);

07-28 01:31