【问题标题】:How can I put 4 colors in linearlayout如何在线性布局中放置 4 种颜色
【发布时间】:2020-01-01 14:53:01
【问题描述】:

您能否指导我如何在线性布局中放置 4 种颜色,而不是创建 4 个线性布局视图,因为我意识到有更好的方法,但我不知道如何应用它。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

结果应该如下图所示

【问题讨论】:

  • 相对于内存和资源消耗的约束布局优于线性布局。您可以使用答案中的约束布局

标签: android


【解决方案1】:

最好用ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <ImageView
            android:id="@+id/imgOne"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#ff00"
            app:layout_constraintBottom_toTopOf="@+id/imgThree"
            app:layout_constraintEnd_toStartOf="@id/imgTwo"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


    <ImageView
            android:id="@+id/imgTwo"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#2196F3"
            app:layout_constraintBottom_toTopOf="@+id/imgFour"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@id/imgOne"
            app:layout_constraintTop_toTopOf="parent" />


    <ImageView
            android:id="@+id/imgThree"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#FFC107"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/imgTwo"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imgOne" />


    <ImageView
            android:id="@+id/imgFour"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#FF5722"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@id/imgThree"
            app:layout_constraintTop_toBottomOf="@+id/imgTwo" />


</androidx.constraintlayout.widget.ConstraintLayout>

输出

【讨论】:

  • 好主意,但我试图避免创建许多元素,如果那是不可能的,那么我会应用你的风格。
  • 嗨..你能帮帮我吗 --> stackoverflow.com/questions/59526045/…
  • @Wini 让我检查一下
【解决方案2】:

像这样使用约束布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/dark_blue"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/clThird"
        app:layout_constraintEnd_toStartOf="@id/clSecond"
        app:layout_constraintStart_toStartOf="parent"
        android:id="@+id/clFirst">

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/light_gray"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/clFourth"
        app:layout_constraintStart_toEndOf="@id/clFirst"
        android:id="@+id/clSecond">

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/clFourth"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/clFirst"
        android:id="@+id/clThird">

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/blue_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/clSecond"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/clThird"
        android:id="@+id/clFourth">

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    【解决方案3】:

    您可以将线性布局用于垂直和水平方向,权重为 1,如下所示

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@color/grey1" />
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@color/grey2" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@color/grey3" />
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@color/grey4" />
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2021-10-31
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    相关资源
    最近更新 更多