【问题标题】:How to create a 3x3 grid with sub child grids 2x2 and 1x1?如何创建具有子子网格 2x2 和 1x1 的 3x3 网格?
【发布时间】:2018-03-15 18:41:24
【问题描述】:

目前我正在使用嵌套的线性布局和嵌套的 weightsum 和布局权重来实现我在下面的屏幕截图中想要的。

但是,我确实注意到,正如 lint 警告所暗示的那样,我的一部过时手机有点慢。因为嵌套的权重总和似乎会阻碍设备的性能。

这是我目前的线性布局代码。我想实现所附的屏幕截图。有没有其他方法可以在不使用广泛嵌套的权重和的情况下实现它?我见过做类似事情的约束布局,但我如何实际使用它?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/title_layout"
    android:weightSum="3"
    android:orientation="vertical"
    android:layout_above="@id/next">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:weightSum="3"
        android:layout_weight="2">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#345678"
            android:layout_weight="2">

        </RelativeLayout>

        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:weightSum="2"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <RelativeLayout
                android:background="#3bcad5"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
            </RelativeLayout>

            <RelativeLayout
                android:background="#3ca3d2"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
            </RelativeLayout>

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:weightSum="3"
        android:layout_weight="1">

        <RelativeLayout
            android:background="#123456"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

        </RelativeLayout>

        <RelativeLayout
            android:background="#310911"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

        </RelativeLayout>

        <RelativeLayout
            android:background="#92F358"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">

        </RelativeLayout>

    </LinearLayout>
</LinearLayout>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您可以使用 ConstraintLayout 设置上述布局,如下所示:

    my_file.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@android:color/darker_gray"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0">
    
    
    
    
            <android.support.constraint.Guideline
                android:id="@+id/guideline"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.6666667" />
    
            <View
                android:id="@+id/view_white"
                app:layout_constraintStart_toEndOf="@id/guideline"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintDimensionRatio="1"
                android:background="@android:color/white"/>
    
    
            <View
                android:id="@+id/view_green"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintDimensionRatio="1"
                android:background="@android:color/holo_green_dark"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toStartOf="@id/guideline"
            />
    
            <View
                android:id="@+id/view_orange"
                app:layout_constraintTop_toBottomOf="@+id/view_white"
                app:layout_constraintStart_toEndOf="@id/guideline"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintDimensionRatio="1"
                android:background="@android:color/holo_orange_dark"/>
    
            <View
                android:id="@+id/view_red"
                app:layout_constraintTop_toBottomOf="@+id/view_orange"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintDimensionRatio="1"
                android:background="@android:color/holo_red_dark"/>
    
            <View
                android:id="@+id/view_purple"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@android:color/holo_purple"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintDimensionRatio="1"
                app:layout_constraintEnd_toStartOf="@+id/view_red"
                app:layout_constraintTop_toBottomOf="@+id/view_green" />
    
            <View
                android:id="@+id/view_blue"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@android:color/holo_blue_light"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintDimensionRatio="1"
                app:layout_constraintTop_toBottomOf="@+id/view_green" />
    
    
        </android.support.constraint.ConstraintLayout>
    
    
    </android.support.constraint.ConstraintLayout>
    

    主约束布局设置为正方形,第二个约束布局创建所有其他视图。这和你的屏幕截图一模一样

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-26
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 2019-11-17
      • 2023-03-07
      相关资源
      最近更新 更多