导航栏后面的Mediacontroller

时间:2014-07-16 23:39:21

标签: android

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="tk.ejb1123.quickflix.VideoPayer"
android:background="@android:color/black"
android:id="@+id/layoutf">


<VideoView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/videoView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:visibility="visible" />

 @Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {

        View decorView;
        decorView = findViewById(R.id.videoView);
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                       // | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        );}
}

链接到图片示例:http://imgur.com/Zq6Cq7M

enter image description here

我尝试改变引力并且它似乎不起作用,我在互联网上找不到任何东西来解决这个问题。 mediaContoller锚定到R.id.videoView。我不知道这在其他应用程序中是如何工作的,比如玩书。

2 个答案:

答案 0 :(得分:0)

我最近遇到了同样的问题,我没有找到任何解决问题的好方法,但是解决方法:

首先获取navigation_bar_height:

int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
    mNavigationBarSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, getResources().getDimension(resourceId), resources.getDisplayMetrics());
}

然后,您可以向mediaController添加一个监听器,类似的东西(mRelativeLayout是我的视频视图的父级):

mMediaController.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
    @Override
    public void onViewAttachedToWindow(View view) {
        ((FrameLayout.LayoutParams) mRelativeLayout.getLayoutParams()).bottomMargin = mNavigationBarSize;
        mRelativeLayout.requestLayout();
    }

    @Override
    public void onViewDetachedFromWindow(View view) {
        hideSystemUI();
        ((FrameLayout.LayoutParams) mRelativeLayout.getLayoutParams()).bottomMargin = 0;
        mRelativeLayout.requestLayout();
    }
});

我希望它能帮到你

修改:如果您的活动有&#34;白色主题&#34;,您在切换时会看到一点图形故障,您可能需要在此活动的主题中添加:<item name="android:windowBackground">@null</item> (或以编程方式进行)

答案 1 :(得分:0)

我通过设置Gravity和Margins来获得解决方案。

布局文件

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_sample_video);

    ColorDrawable drawable = new ColorDrawable(Color.argb(100,0,0,0));
    getSupportActionBar().setBackgroundDrawable(drawable);
    getSupportActionBar().setTitle("Big Bunny");

    spinner = (ProgressBar)findViewById(R.id.my_spinner);

    dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    lpp = new FrameLayout.LayoutParams(dm.widthPixels, 170);
    lpp.gravity= Gravity.BOTTOM;
    lpp.setMargins(0,0,0,100);
    video = (VideoView)findViewById(R.id.videoSurface);
    video.setVideoPath("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
    video.setMediaController(mediaController = new MediaController(this){
        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK ) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    return true;
                } else if (event.getAction() == KeyEvent.ACTION_UP) {
                    ((Activity) getContext()).onBackPressed();
                    return true;
                }
            }
            return super.dispatchKeyEvent(event);
        }
    });
    hideSystemUI();
    mediaController.setLayoutParams(lpp);
    mediaController.setAlpha(0.5f);
    video.start();
    video.setOnCompletionListener(this);
    video.setOnInfoListener(this);
}

现在,在OnCreate()上设置MediaController布局并使用setLayoutParams()分配相同的内容。

@Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        lpp = new FrameLayout.LayoutParams(dm.widthPixels, 170);
        //lpp.gravity= Gravity.BOTTOM;
        //lpp.setMargins(0,0,0,100);
        mediaController.setLayoutParams(lpp);
        //Toast.makeText(getApplicationContext(),"Landscape",Toast.LENGTH_LONG).show();
    } 
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        lpp = new FrameLayout.LayoutParams(dm.widthPixels, 170);
        lpp.gravity= Gravity.BOTTOM;
        lpp.setMargins(0,0,0,100);
        mediaController.setLayoutParams(lpp);
        //Toast.makeText(getApplicationContext(),"Portrait",Toast.LENGTH_LONG).show();
    }
}

如果需要,您还可以使用以下代码处理方向更改。

{{1}}