2012-10-05 21 views
0

我有這樣的佈局:的Android SurfaceView把所有的空間佈局

<LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical"> 


       <SurfaceView 
        android:id="@+id/Preview" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_marginBottom="20dp" 
        android:layout_marginLeft="20dp" 
        android:layout_marginRight="20dp" /> 


       <VideoView 
        android:id="@+id/videoView" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_marginBottom="20dp" 
        android:layout_marginLeft="20dp" 
        android:layout_marginRight="20dp"/> 
      </LinearLayout> 

的SurfaceView和VideoView應該平分的可用空間,並且他們做,如果我把VideoView第一。當我像上面那樣做時,SurfaceView將佔據佈局中的所有空間。 如何避免這種情況,而不給出具體的寬度和高度DPS?

回答

0

無論您Views都填充母作爲屬性指出:android:layout_width=fill_parent

平分屏幕,以便您兩種觀點是彼此相鄰,您可以使用權:

 <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 


      <SurfaceView 
       android:id="@+id/Preview" 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_marginBottom="20dp" 
       android:layout_marginLeft="20dp" 
       android:layout_marginRight="20dp" 
       android:layout_weight="1" /> 


      <VideoView 
       android:id="@+id/videoView" 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_marginBottom="20dp" 
       android:layout_marginLeft="20dp" 
       android:layout_marginRight="20dp" 
       android:layout_weight="1" /> 
     </LinearLayout> 
0

你必須add layout_weight = 1

<LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical"> 


       <SurfaceView 
        android:id="@+id/Preview" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_marginBottom="20dp" 
        android:layout_marginLeft="20dp" 
        android:layout_marginRight="20dp" 
        android:layout_weight="1"/> 


       <VideoView 
        android:id="@+id/videoView" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_marginBottom="20dp" 
        android:layout_marginLeft="20dp" 
        android:layout_marginRight="20dp" 
        android:layout_weight="1"/> 
      </LinearLayout> 
+0

這使得視頻可見,但非常小。如果我增加重量,視頻會變得更大,但不幸的是,從未真正佔用過這個空間。 感謝您的想法! – FWeigl

+0

你必須刪除邊距,同時videoView保持視頻的寬高比,所以在大多數時間它不會填滿所有可用空間。 –

0

試着用「weightSum = 2」。也在兩個視圖中分割邊距!

<LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:layout_marginLeft="20dp" 
       android:layout_marginRight="20dp" 
       android:weightSum="2"> 


      <SurfaceView 
       android:id="@+id/Preview" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_marginBottom="10dp" 
       android:layout_weight="1"/> 


      <VideoView 
       android:id="@+id/videoView" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_marginTop="10dp" 
       android:layout_marginBottom="20dp" 
       android:layout_weight="1"/> 
     </LinearLayout>