【问题标题】:Nested Linear Layout in Android to evenly share height and widthAndroid中的嵌套线性布局以均匀共享高度和宽度
【发布时间】:2016-10-26 09:07:49
【问题描述】:

我在我的测试 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>

【问题讨论】:

  • 我建议改用 GridLayout
  • 每个surfaceView负责播放一个视频。我在使用网格布局时遇到了一些问题。

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


【解决方案1】:

为内部LinearLayout设置layout_weight如下:

<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

【讨论】:

  • 这行得通!网格布局将是理想的,因为我会随时更改单元格的数量。但是每个surfaceView都负责播放一个视频。我在快速测试中使用网格布局时没有播放视频
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2020-05-07
  • 2020-12-31
  • 2021-05-03
  • 2023-03-22
  • 1970-01-01
相关资源
最近更新 更多