2013-03-18 109 views
2

我正在嘗試將線性佈局內的視頻視圖拉伸至全屏,但應用程序不會將視頻視圖的寬度拉伸至與android屏幕相同。這裏是我的代碼Android全寬視頻視圖

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="720dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_gravity="fill_horizontal" 
    android:layout_marginTop="140dp" > 

<VideoView 
    android:id="@+id/videoView1" 
    android:layout_width="fill_parent" 
    android:layout_height="720dp" /> 

</LinearLayout> 

左右各有10到15個dp空間。另外線性佈局不能延伸到全屏。我希望它在寬度上填充屏幕。

+1

http://stackoverflow.com/a/12335916/598170 – Som 2013-03-18 06:55:25

+0

只有這篇文章http://blog.kasenlam.com/2012/02/android-how-to-stretch-video-to-fill.html解決了我的問題 – 2016-01-22 11:02:31

+0

@Taimur它是特定於'RelativeLayout'不適用於其他佈局 – 2016-01-22 11:12:07

回答

4

可以使用顯示器矩陣

    DisplayMetrics displaymetrics = new DisplayMetrics(); 
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
        int h = displaymetrics.heightPixels; 
        int w = displaymetrics.widthPixels; 

現在使用這個高度得到屏幕的當前高度和寬度,你的視頻視圖的寬度:) 以編程方式使用它不像720dp和所有硬編碼。 getWidth()和getHeight()被谷歌棄用

+0

實際上@ rawD3buGGG3R的答案適用於我,我會建議以編程方式設置參數以達到想要的效果。 – 2013-03-21 05:08:33

+0

:)是的,請發給我代碼 – 2013-03-21 10:41:23

1

獲得運行時的高度和佈局的寬度,並設置在VideoView

Display display = getWindowManager().getDefaultDisplay(); 
        int width = display.getWidth(); 
        int height = display.getHeight(); 

       // videoview.setLayoutParams(new FrameLayout.LayoutParams(550,550)); 

        videoview.setLayoutParams(new FrameLayout.LayoutParams(width,height)); 

//FrameLayout : write your layout name which you used 
0

嘗試使用Fill_parent而不是使用特定的高度。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linearLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_alignParentBottom="true" 
android:layout_alignParentLeft="true" 
android:layout_alignParentRight="true" 
android:layout_alignParentTop="true" 
android:layout_gravity="fill_horizontal" > 

<VideoView 
android:id="@+id/videoView1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" /> 

</LinearLayout> 
+0

這不適用於LinearLayout – soshial 2017-10-20 14:15:32

3

其實沒有必要設置寬度編程

android:layout_width="match_parent" 
android:layout_alignParentLeft="true" 
android:layout_alignParentRight="true" 

應該足以填補全寬視頻視圖容器。並使用match_parent而不是fill_parent。希望這有助於某人。

+1

如果您必須在運行時更改displaymetrics,該怎麼辦? – 2014-09-04 09:53:48