如何在约束布局内以水平布局显示颜色

时间:2019-07-07 10:27:14

标签: android-linearlayout android-constraintlayout

我正在使用约束布局,如下代码所示。如下图所示,我想添加到水平方向的LinearLayout中,以便左右各部分都有特定的颜色。 但是,根据下面的代码,线性布局无法正确划分,并且永远不会显示为每个划分分配的颜色。

如何解决线性布局问题并显示每个颜色

布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout   
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">   

    <Button       
        android:id="@+id/topBtn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="topBtn1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@id/topBtn_2"
        app:layout_constraintBottom_toTopOf="@id/fragmentContainer"
        android:onClick="clicksHandler"/>

    <Button
        android:id="@+id/topBtn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="topBtn2"
        app:layout_constraintLeft_toRightOf="@id/topBtn_1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@id/topBtn_3"
        app:layout_constraintBottom_toTopOf="@id/fragmentContainer"
        android:onClick="clicksHandler"/>
     <Button
        android:id="@+id/topBtn_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="topBtn3"
        app:layout_constraintLeft_toRightOf="@id/topBtn_2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@id/fragmentContainer"
        android:onClick="clicksHandler"/>  

    <LinearLayout
        android:id="@+id/fragmentContainer"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/topBtn_1"
        app:layout_constraintBottom_toTopOf="@id/BottomBtn_1">

        <FrameLayout
            android:id="@+id/fragOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_light">
        </FrameLayout>

        <FrameLayout
            android:id="@+id/fragTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_orange_light">
        </FrameLayout>

    </LinearLayout>

    <Button
        android:id="@+id/BottomBtn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BottomBtn1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/BottomBtn_2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/fragmentContainer"
        android:onClick="clicksHandler"/>

    <Button
        android:id="@+id/BottomBtn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BottomBtn2"
        app:layout_constraintLeft_toRightOf="@id/BottomBtn_1"
        app:layout_constraintRight_toLeftOf="@id/BottomBtn_3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/fragmentContainer"
        android:onClick="clicksHandler"/>
    <Button
        android:id="@+id/BottomBtn_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BottomBtn3"
        app:layout_constraintLeft_toRightOf="@id/BottomBtn_2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/fragmentContainer"
        android:onClick="clicksHandler"/>

</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案