旋转android设备时如何设置全屏视频
Posted
技术标签:
【中文标题】旋转android设备时如何设置全屏视频【英文标题】:how to set full screen video when rotating android device 【发布时间】:2016-10-14 05:32:19 【问题描述】:我正在使用视频视图播放外部卡中的 MP4 视频文件。当我旋转我的 android 设备视频时不显示全屏。我正在使用 android 4.4 正常,包括下面的 ui 输出屏幕 当我旋转手机时
<LinearLayout 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_
android:layout_
android:id="@+id/video"
android:orientation="vertical"
android:background="@drawable/planebackground">
<android.support.design.widget.AppBarLayout
android:layout_
android:layout_
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/videotoolbar"
android:layout_
android:layout_
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="@+id/layout1"
android:layout_
android:layout_
>
<VideoView
android:id="@+id/VideoView"
android:layout_
android:layout_
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_
android:layout_
android:layout_below="@+id/layout1"
android:weightSum="1">
<TextView
android:text="About Course"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="16sp"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:layout_marginTop="70dp"
android:id="@+id/aboutcuz" />
<TextView
android:text="TextView"
android:layout_
android:layout_
android:layout_marginTop="48dp"
android:id="@+id/about_course_text"
android:fontFamily="Arial"
android:layout_below="@+id/aboutcuz"
android:layout_alignLeft="@+id/aboutcuz"
android:layout_alignStart="@+id/aboutcuz"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:gravity="center"
/>
<TextView
android:text="TextView"
android:layout_
android:layout_
android:lines="2"
android:scrollHorizontally="true"
android:ellipsize="end"
android:id="@+id/coursenamev"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/aboutcuz"
android:layout_alignStart="@+id/aboutcuz"
android:layout_marginTop="18dp" />
<View
android:layout_
android:layout_
android:layout_marginTop="60dp"
android:background="@android:color/darker_gray"/>
<View
android:layout_
android:layout_
android:layout_below="@+id/aboutcuz"
android:layout_marginTop="10dp"
android:background="@android:color/darker_gray"/>
</RelativeLayout>
</LinearLayout>
我的代码中是否存在布局问题
【问题讨论】:
请看***.com/questions/11310764/… 不适合我 【参考方案1】:请试试这个。我希望这可能会产生您想要的结果。
<LinearLayout 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_
android:layout_
android:id="@+id/video"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_
android:layout_
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/videotoolbar"
android:layout_
android:layout_
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="@+id/layout1"
android:layout_
android:layout_
>
<VideoView
android:id="@+id/VideoView"
android:layout_
android:layout_
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_
android:layout_
android:layout_below="@+id/layout1"
android:weightSum="1">
<TextView
android:text="About Course"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="16sp"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:layout_marginTop="70dp"
android:id="@+id/aboutcuz" />
<TextView
android:text="TextView"
android:layout_
android:layout_
android:layout_marginTop="48dp"
android:id="@+id/about_course_text"
android:fontFamily="Arial"
android:layout_below="@+id/aboutcuz"
android:layout_alignLeft="@+id/aboutcuz"
android:layout_alignStart="@+id/aboutcuz"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:gravity="center"
/>
<TextView
android:text="TextView"
android:layout_
android:layout_
android:lines="2"
android:scrollHorizontally="true"
android:ellipsize="end"
android:id="@+id/coursenamev"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/aboutcuz"
android:layout_alignStart="@+id/aboutcuz"
android:layout_marginTop="18dp" />
<View
android:layout_
android:layout_
android:layout_marginTop="60dp"
android:background="@android:color/darker_gray"/>
<View
android:layout_
android:layout_
android:layout_below="@+id/aboutcuz"
android:layout_marginTop="10dp"
android:background="@android:color/darker_gray"/>
</RelativeLayout>
【讨论】:
我需要全屏播放视频【参考方案2】:制作您的自定义 VideoView 类
public class FullScreenVideoView extends VideoView
public FullScreenVideoView(Context context)
super(context);
public FullScreenVideoView(Context context, AttributeSet attrs)
super(context, attrs);
public FullScreenVideoView(Context context, AttributeSet attrs, int defStyle)
super(context, attrs, defStyle);
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
public void setFixedVideoSize(int width, int height)
getHolder().setFixedSize(width, height);
并在您的 Layout 中设置 VideoView
<yourpackage.FullScreenVideoView
android:id="@+id/VideoView"
android:layout_
android:layout_
/>
【讨论】:
【参考方案3】:做这样的事情
@Override
public void onConfigurationChanged(Configuration newConfig)
super.onConfigurationChanged(newConfig);
Logger.debug(TAG, "onConfigChanged");
updateLayout(newConfig.orientation);
/**
* Update the current layout params according to current device orientation.
*/
private void updateLayout(int orientation)
LinearLayout.LayoutParams favParams = (LinearLayout.LayoutParams) mFavoriteLayout.getLayoutParams();
LinearLayout.LayoutParams historyParams = (LinearLayout.LayoutParams) mHistoryLayout.getLayoutParams();
LinearLayout.LayoutParams colParams = (LinearLayout.LayoutParams) mColumnLayout.getLayoutParams();
if (orientation == Configuration.ORIENTATION_PORTRAIT)
mColumnLayout.setOrientation(LinearLayout.VERTICAL);
historyParams.width = favParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
historyParams.height = favParams.height = 0;
colParams.weight = 1.0f;
else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
mColumnLayout.setOrientation(LinearLayout.HORIZONTAL);
historyParams.height = favParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
historyParams.width = favParams.width = 0;
colParams.weight = 2.0f;
// Set back updated params and redraw
mFavoriteLayout.setLayoutParams(favParams);
mHistoryLayout.setLayoutParams(historyParams);
mColumnLayout.setLayoutParams(colParams);
mColumnLayout.invalidate();
当你改变方向改变重量,你就能达到你想要的。
【讨论】:
【参考方案4】:这很简单。您可以在创建新的“全屏活动”时找到一个示例。
是这样的:
// Hide UI first
ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
actionBar.hide();
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
【讨论】:
以上是关于旋转android设备时如何设置全屏视频的主要内容,如果未能解决你的问题,请参考以下文章