Android中的嵌套线性布局可均匀分享高度和宽度

时间:2016-06-23 20:53:26

标签: android android-layout layout android-linearlayout surfaceview

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

基本上我想要任意数量的网格(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>

1 个答案:

答案 0 :(得分: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

相关问题