0

我在我的测试Android应用程序中实现一个2x2网格(一个垂直和一个水平)的嵌套线性布局,但我有问题使单元均匀填充整个屏幕。目前我手动将高度设置为任意数字(150dp)。我怎样才能修复它,并使网格单元之间的高度和宽度均匀分配?在Android中嵌套线性布局以均匀分享高度和宽度

基本上我想要任何数量的可能的网格我有(2x3,3x3等)均匀分享屏幕? (。每一个surfaceView负责播放视频我不得不使用网格布局的一些问题)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearlayout_0" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/linearlayout_10" 
     android:layout_width="fill_parent" 
     android:layout_height="150dp" 
     android:orientation="horizontal" > 

     <SurfaceView 
      android:id="@+id/video_11_surfaceview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

     <SurfaceView 
      android:id="@+id/video_12_surfaceview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearlayout_11" 
     android:layout_width="match_parent" 
     android:layout_height="150dp" 
     android:orientation="horizontal" > 

     <SurfaceView 
      android:id="@+id/video_21_surfaceview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

     <SurfaceView 
      android:id="@+id/video_22_surfaceview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 
    </LinearLayout> 

</LinearLayout> 
+2

我会建议使用GridLayout代替 – SillyFidget

+0

每个surfaceView负责播放视频。使用网格布局时遇到了一些问题。 –

回答

1

设置layout_weight为内LinearLayout如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linearlayout_0" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<LinearLayout 
    android:id="@+id/linearlayout_10" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 

    <SurfaceView 
     android:id="@+id/video_11_surfaceview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <SurfaceView 
     android:id="@+id/video_12_surfaceview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/linearlayout_11" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 

    <SurfaceView 
     android:id="@+id/video_21_surfaceview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <SurfaceView 
     android:id="@+id/video_22_surfaceview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
</LinearLayout> 

这工作,但你可能想看看GridLayout

+0

这个工程!网格布局将是理想的,因为我会随时更改单元的数量。但每个surfaceView都有责任播放视频。我在快速测试中使用网格布局时未播放视频 –

相关问题