ConstraintLayout,最佳做法,带状背景

时间:2019-04-05 11:49:35

标签: android kotlin androidx

我正在创建一个背景(如图所示),该背景具有3种颜色,均分(相同的宽度)。

enter image description here

今天我正在使用以下代码:

  <LinearLayout
            android:layout_marginLeft="@dimen/default_margin"
            android:layout_marginRight="@dimen/default_margin"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="4dp">

        <View 
            android:layout_width="0dp" 
            android:layout_height="match_parent" 
            android:layout_weight="1" 
            android:background="@color/colorBlack"/>
        <View 
            android:layout_width="0dp" 
            android:layout_height="match_parent" 
            android:layout_weight="1" 
            android:background="@color/colorGreen"/>
        <View 
            android:layout_width="0dp" 
            android:layout_height="match_parent" 
            android:layout_weight="1" 
            android:background="@color/colorRed"/>
  </LinearLayout>

如您所见,我正在使用LinearLayout和三个View及其权重的组合。 自从我开始使用ConstraintLayout以来,我一直在尝试减少XML的Views数量,因此我相信可以减少这个问题。

我尝试创建3px x 1px的图像作为背景,但是当我使用fitXY时,它会变成一个渐变。

是否有一种解决方案,涉及使用单个View来创建此背景(相同的宽度和三种不同的颜色)?

3 个答案:

答案 0 :(得分:2)

您可以使用guidelineschains,为此,我想使用链条:

在下面的示例中,我有3个带有水平链的按钮-它们的宽度相同,所以现在要做的就是为它们设置背景颜色。

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="4dp"
        android:text="Button"
        android:background="@color/black"
        app:layout_constraintBottom_toBottomOf="@+id/button"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/button" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="4dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:text="Button"
        android:background="@color/some"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="4dp"
        android:text="Button"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="@+id/button" />


</androidx.constraintlayout.widget.ConstraintLayout>

它看起来像这样:

enter image description here

有关更多信息,您可以检查ConstraintLayoutGuidelines

答案 1 :(得分:2)

选中此简单的解决方案,以了解它会满足您的要求

<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">

    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="4dp"
        android:background="#000000"
        app:layout_constraintEnd_toStartOf="@id/view2"
        app:layout_constraintStart_toStartOf="parent" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="4dp"
        android:background="#00FF00"
        app:layout_constraintEnd_toStartOf="@id/view3"
        app:layout_constraintStart_toEndOf="@id/view1"
        app:layout_constraintTop_toTopOf="@id/view1" />

    <View
        android:id="@+id/view3"
        android:layout_width="0dp"
        android:layout_height="4dp"
        android:background="#FF0000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/view2"
        app:layout_constraintTop_toTopOf="@id/view1" />

</androidx.constraintlayout.widget.ConstraintLayout>

答案 2 :(得分:0)

<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_marginTop="@dimen/margin_10_dp"
android:layout_height="wrap_content">

<View
    android:id="@+id/view1"
    android:layout_width="0dp"
    android:layout_height="5dp"
    android:background="#212121"
    app:layout_constraintEnd_toStartOf="@+id/view2"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintStart_toStartOf="parent" />

<View
    android:id="@+id/view2"
    android:layout_width="0dp"
    android:layout_height="5dp"
    android:background="#3cff42"
    app:layout_constraintEnd_toStartOf="@+id/view3"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintStart_toEndOf="@+id/view1" />

<View
    android:id="@+id/view3"
    android:layout_width="0dp"
    android:layout_height="5dp"
    android:background="#f93d3d"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintStart_toEndOf="@+id/view2" />

</android.support.constraint.ConstraintLayout>
相关问题