我有一个播放实时流视频的表面视图.变成横向时,我需要使其全屏显示.我有一个活动,其中包含一个片段,其中包含surfaceView和显示其他实时流的gridview.这是片段的布局-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
>
<RelativeLayout
android:id="@+id/guiLayout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
android:layout_weight="0.6">
<FrameLayout
android:id="@+id/videoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp">
<SurfaceView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="visible">
</SurfaceView>
</FrameLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/layoutGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:background="@android:color/white">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="2dp"
android:isScrollContainer="false"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp"
android:background="@color/transparent"
android:layout_gravity="center_horizontal|bottom" />
</RelativeLayout>
因此,我在活动中的onConfigChanged调用了超级onConfigChanged,然后调用了片段onRotate方法并将其传递给newConfig-
public void onRotate(Configuration newConfig)
{
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Log.e(TAG, "CHANGING");
getView().findViewById(R.id.guiLayout).setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
if(gridView != null)
gridView.setVisibility(View.GONE);
getView().findViewById(R.id.layoutGrid).setVisibility(View.GONE);
}
else
{
Log.e(TAG, "CHANGING PORTRAIT");
getView().findViewById(R.id.guiLayout).setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT ,0.6f));
if(gridView != null)
gridView.setVisibility(View.VISIBLE);
else
gridView = (GridView) getView().findViewById(R.id.gridView);
}
这个想法是,如果我将guiLayout的大小调整为全屏,则其子项应该更改为适合的大小,因为它们设置为MATCH_PARENT.但是情况并非如此,引导布局可调整为全屏显示,但孩子们仍然占据了屏幕的40%,这是原始重量所决定的.我尝试了许多不同的方法来让孩子们与父母相匹配,但这没有用.任何关于我做错事情的建议将不胜感激.
解决方法:
到全屏:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
videoLayout.setBackgroundColor(Color.BLACK);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
ViewGroup.LayoutParams videoLayoutParams = videoLayout.getLayoutParams();
videoLayoutParams.width = displayMetrics.widthPixels;
videoLayoutParams.height = displayMetrics.heightPixels;
ViewGroup.LayoutParams videoParams = videoView.getLayoutParams();
videoParams.width = displayMetrics.widthPixels;
videoParams.height = displayMetrics.heightPixels;