ConstraintLayout、最佳实践、带状背景
ConstraintLayout, best practices, banded background
我正在创建一个背景(如图),它有 3 种颜色(宽度相同)。
今天我使用以下代码:
<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 和三个具有权重的视图。 自从我开始使用 ConstraintLayout 以来,我一直在尝试减少 XML 的视图数量,我相信我可以减少这个问题。
我试过创建一个 3px x 1px 的图像作为背景,但是当我使用 fitXY 时它变成了渐变。
是否有涉及使用单个视图创建此背景(相同宽度和三种不同颜色)的解决方案?
<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>
您可以使用 guidelines
或 chains
,为此,我想使用链:
在下面的示例中,我有 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>
看起来像这样:
更多信息,您可以查看ConstraintLayout and Guidelines
检查这个简单的解决方案以了解它将满足您的要求
<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>
我正在创建一个背景(如图),它有 3 种颜色(宽度相同)。
今天我使用以下代码:
<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 和三个具有权重的视图。 自从我开始使用 ConstraintLayout 以来,我一直在尝试减少 XML 的视图数量,我相信我可以减少这个问题。
我试过创建一个 3px x 1px 的图像作为背景,但是当我使用 fitXY 时它变成了渐变。
是否有涉及使用单个视图创建此背景(相同宽度和三种不同颜色)的解决方案?
<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>
您可以使用 guidelines
或 chains
,为此,我想使用链:
在下面的示例中,我有 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>
看起来像这样:
更多信息,您可以查看ConstraintLayout and Guidelines
检查这个简单的解决方案以了解它将满足您的要求
<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>