【问题标题】:VideoView does not fill the entire screenVideoView 没有填满整个屏幕
【发布时间】:2017-06-02 19:41:29
【问题描述】:

我正在尝试为我的应用播放一段简短的介绍视频。既然是游戏,首先我创建了一个主题来隐藏操作栏并全屏显示。

styles.xml:

<style name="AppTheme.NoTitleBar" parent="AppTheme">
    <item name="android:windowNoTitle">true</item>
</style>

<style name="AppTheme.NoTitleBar.Fullscreen" parent="AppTheme.NoTitleBar">
    <item name="android:windowFullscreen">true</item>
</style>

然后,我创建了一个只有 VideoView 的布局:

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black"
    tools:context=".MainActivity" >

    <VideoView
        android:id="@+id/videoView_intro"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp" />

</RelativeLayout>

最后,我通过以下代码播放了视频:

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    VideoView v = (VideoView) findViewById(R.id.videoView_intro);
    v.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.intro));
    /* This line had a code snippet for event handling, unrelated to this question */
    v.start();
}

我成功播放了视频,但 VideoView 底部的边距很小。我认为它与通知栏的高度相匹配。我怎样才能摆脱那个边距?

编辑:我不知道是否需要此信息,但该活动被强制为横向。另外,我针对 API 级别 8 的所有 android 设备,因此该解决方案必须与 API 级别 8 兼容。

AndroidManifest.xml:

    <activity
        android:name="org.package.name.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape"
        android:theme="@style/AppTheme.NoTitleBar.Fullscreen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

【问题讨论】:

  • 我想最快的方法是应用带有负值的底部边距。试试看
  • @Maver1ck 尝试过android:layout_marginBottom="-8dp"。结果是否定的。

标签: android


【解决方案1】:

您最好的选择可能是创建您自己的 VideoView 子类,覆盖 onMeasure(int, int) 方法并获取屏幕尺寸并修改 VideoView 高度/宽度以匹配您设备的正确高度/宽度。

如果您不希望这样做,那么您可能更容易的其他选项是在全屏对话框中启动视频,或者您可以将 LayoutParams 设置为运行时生成的静态大小:

VideoView v = (VideoView) findViewById(R.id.videoView_intro);
v.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.intro));

DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    v.setLayoutParams(new LayoutParams(metrics.widthPixels, metrics.heightPixels));

v.start();

【讨论】:

  • 其实我的问题原因在其他地方,但您的回答有助于了解系统功能。但是,由于代码引入了程序上的歧义,我提出了修改。
  • @ArleCamille 哦,您可能已经从不同的包中导入了 LayoutParams。我不确定我是否正确接受了编辑...
【解决方案2】:

好吧,当我想出这个想法时,我想我是个傻瓜。

问题不在于通知栏。相反,它与视频的宽高比有关。

视频大小为 480×272。它的纵横比约为。 1.7647。另一方面,Galaxy Nexus 的可用屏幕尺寸为 1184×720,纵横比约为1.6444。所以,基本上视频比屏幕“长”。

我没有找到原因,因为: 1)奇怪的是边距的高度几乎与通知栏的高度匹配,并且 2)由于视频是一个比手机屏幕更窄的系统的记录,我不相信这是原因。 (编码器打破了假定的纵横比。)

最后,我应该研究的是这篇文章:Center VideoView in landscape mode

教训是,不要过多地进入你的假设。

【讨论】:

    【解决方案3】:
    VideoView v = (VideoView) findViewById(R.id.videoView_intro);
    v.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.intro));
    
    DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        v.setLayoutParams(new LayoutParams(metrics.widthPixels, metrics.heightPixels));
    
    v.start();`enter code here`
    
    this code worked correctly
    

    【讨论】:

    • 嗨,您应该真正解释一下为什么您的答案比公认的更好。尤其是在发布这么旧的答案时。
    • 实际上我不知道在哪里为那个旧答案写评论..所以,我将其添加为新答案
    • 您需要获得更多声望才能发表评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多