我正在尝试为我的应用制作视频播放器,但是当我旋转手机时,突然所有视频视图都向左移动。这是我的 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WatchEpisode">
<LinearLayout
android:id="@+id/ExtraLayout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/PlayerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<VideoView
android:id="@+id/ChapterVideoView"
android:layout_width="0dp"
android:layout_height="200dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/VideoControls"
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/ChapterTitleText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="18dp"
android:layout_marginEnd="10dp"
android:text="TextView"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/ServerBtn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/FullscreenBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:layout_marginEnd="18dp"
android:background="@android:color/transparent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_fullscreen_open" />
<ImageButton
android:id="@+id/SubtitlesBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:layout_marginEnd="10dp"
android:background="@android:color/transparent"
app:layout_constraintEnd_toStartOf="@+id/FullscreenBtn"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_subtitles_on" />
<ImageButton
android:id="@+id/ChapterSettingsBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:layout_marginEnd="10dp"
android:background="@android:color/transparent"
app:layout_constraintEnd_toStartOf="@+id/SubtitlesBtn"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_settings" />
<ImageButton
android:id="@+id/ServerBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:layout_marginEnd="10dp"
android:background="@android:color/transparent"
app:layout_constraintEnd_toStartOf="@+id/ChapterSettingsBtn"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_cloud_2" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/ReplayBtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="25dp"
android:layout_weight="1"
app:srcCompat="@drawable/ic_replay_10" />
<ImageView
android:id="@+id/PlayPauseBtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="25dp"
android:layout_weight="1"
app:srcCompat="@drawable/ic_pause" />
<ImageView
android:id="@+id/ForwardBtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="25dp"
android:layout_weight="1"
app:srcCompat="@drawable/ic_forward_10" />
</LinearLayout>
<SeekBar
android:id="@+id/ProgressSeekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:foregroundTint="@color/ykblue"
android:indeterminateTint="@color/ykblue"
android:progressTint="@color/ykblue"
android:secondaryProgressTint="@color/ykblue"
android:thumbTint="@color/ykblue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/CurrentTimeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginBottom="5dp"
android:text="00:00"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@+id/ProgressSeekBar"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/TotalTimeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginBottom="5dp"
android:text="00:00"
android:textColor="@color/white"
app:layout_constraintBottom_toTopOf="@+id/ProgressSeekBar"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
现在这应该正确居中视频视图,当我在 android studio 上看到它时它实际上正确居中,但我也有一个 onConfigurationChangeListener
因为我希望某些东西在横向或纵向模式下改变.
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
PlayPauseBtn.layoutParams = LinearLayout.LayoutParams(getDps(90), getDps(90))
ReplayBtn.layoutParams = LinearLayout.LayoutParams(getDps(90), getDps(90))
ForwardBtn.layoutParams = LinearLayout.LayoutParams(getDps(90), getDps(90))
FullscreenBtn.setImageResource(R.drawable.ic_fullscreen_exit)
isfullscreen = true
ChapterVideoView.layoutParams = ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)
VideoControls.layoutParams = ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
PlayerLayout.layoutParams = ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
ExtraLayout.layoutParams = ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
} else {
PlayPauseBtn.layoutParams = LinearLayout.LayoutParams(getDps(50), getDps(50))
ReplayBtn.layoutParams = LinearLayout.LayoutParams(getDps(50), getDps(50))
ForwardBtn.layoutParams = LinearLayout.LayoutParams(getDps(50), getDps(50))
FullscreenBtn.setImageResource(R.drawable.ic_fullscreen_open)
isfullscreen = false
ChapterVideoView.layoutParams = ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
VideoControls.layoutParams = ConstraintLayout.LayoutParams(ChapterVideoView.measuredWidth, ChapterVideoView.measuredHeight)
PlayerLayout.layoutParams = ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
ExtraLayout.layoutParams = ConstraintLayout.LayoutParams(ChapterVideoView.measuredWidth, ChapterVideoView.measuredHeight)
}
}
这应该从 match_parent
更改为 wrap_content
视频视图的宽度并将其水平居中,但事实并非如此。它实际上全部向左。有什么想法吗?