【问题标题】:Android SurfaceView taking all the space in layoutAndroid SurfaceView 占据布局中的所有空间
【发布时间】:2012-09-26 18:03:55
【问题描述】:

我有这个布局:

<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,我该如何防止这种情况发生?

【问题讨论】:

    标签: android layout surfaceview


    【解决方案1】:

    您的两个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>
    

    【讨论】:

      【解决方案2】:

      您必须添加 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>
      

      【讨论】:

      • 这使视频可见,但非常小。如果我增加重量,视频会变大到某个点,但不幸的是,从来没有真正占用当前空间。感谢您的想法!
      • 你必须删除边距,而且 videoView 保持视频的纵横比,所以大多数时候它不会填满所有可用空间。
      【解决方案3】:

      尝试使用“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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-04
        • 1970-01-01
        • 2012-01-03
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 1970-01-01
        相关资源
        最近更新 更多