Android 如何使用权重和约束布局分割下面的视图

Android 如何使用权重和约束布局分割下面的视图,android,android-constraintlayout,Android,Android Constraintlayout,我使用LinearLayout创建UI,但我无法将水平灰线居中对齐到圆的中心。我将圆圈和文本放置在垂直线性布局中 找到下面的代码: <LinearLayout android:id="@+id/step_indicator" android:layout_width="match_parent" android:layout_height="75dp" android

我使用LinearLayout创建UI,但我无法将水平灰线居中对齐到圆的中心。我将圆圈和文本放置在垂直线性布局中

找到下面的代码:

<LinearLayout
        android:id="@+id/step_indicator"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:layout_margin="@dimen/margin_10"
        android:gravity="center"
        android:orientation="horizontal">

    <LinearLayout
            android:id="@+id/step1_layout"
            style="@style/StepIndividualLayout">

        <ImageView
                android:id="@+id/step1_imageView"
                style="@style/StepImageView"
                android:layout_gravity="center"
                android:src="@drawable/active"
                app:layout_constraintDimensionRatio="1:1" />

        <TextView
                android:id="@+id/step1_textView"
                style="@style/StepTextView"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Text1"
                android:textColor="@color/dark_blue_color" />
    </LinearLayout>

    <View
            android:id="@+id/step_1_2_view"
            style="@style/StepLine"></View>

    <LinearLayout
            android:id="@+id/step2_layout"
            style="@style/StepIndividualLayout">

        <ImageView
                android:id="@+id/step2_imageView"
                style="@style/StepImageView"
                android:layout_gravity="center"
                android:src="@drawable/step_2_deactivated"
                app:layout_constraintDimensionRatio="1:1" />

        <TextView
                android:id="@+id/step2_textView"
                style="@style/StepTextView"
                android:layout_gravity="center"
                android:text="Text 2" />
    </LinearLayout>

    <View
            android:id="@+id/step_2_3_view"
            style="@style/StepLine"/>
 
</LinearLayout>

上述代码生成以下结果:


您可以根据需要使用CONSTRAITNLYOUT,并为线条提供适当的约束,使其根据图像居中。 此外,我认为在这里链(横向)将使您的生活更容易,为更多的信息

类似这样的内容:


<?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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.2"
        app:layout_constraintHeight_percent="0.1"
        android:text="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view4"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:text="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view5"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.2" />

    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.2"
        app:layout_constraintHeight_percent="0.1"
        android:text="4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view5"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view4"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="2dp"
        android:background="@color/cardview_dark_background"
        app:layout_constraintBottom_toBottomOf="@+id/button2"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toTopOf="@+id/button2" />

    <View
        android:id="@+id/view4"
        android:layout_width="0dp"
        android:layout_height="2dp"
        android:background="@color/cardview_dark_background"
        app:layout_constraintBottom_toBottomOf="@+id/view"
        app:layout_constraintEnd_toStartOf="@+id/button4"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="@+id/view" />

    <View
        android:id="@+id/view5"
        android:layout_width="0dp"
        android:layout_height="2dp"
        android:background="@color/cardview_dark_background"
        app:layout_constraintBottom_toBottomOf="@+id/view"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button4"
        app:layout_constraintTop_toTopOf="@+id/view" />

</androidx.constraintlayout.widget.ConstraintLayout>


现在,只需将按钮替换为自定义视图,即可完成操作。

您可以根据需要使用CONSTRAITNLYOUT,并为线条提供适当的约束,使其根据图像居中。 此外,我认为在这里链(横向)将使您的生活更容易,为更多的信息

类似这样的内容:


<?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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.2"
        app:layout_constraintHeight_percent="0.1"
        android:text="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view4"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:text="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view5"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.2" />

    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="0.2"
        app:layout_constraintHeight_percent="0.1"
        android:text="4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view5"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view4"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="2dp"
        android:background="@color/cardview_dark_background"
        app:layout_constraintBottom_toBottomOf="@+id/button2"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toTopOf="@+id/button2" />

    <View
        android:id="@+id/view4"
        android:layout_width="0dp"
        android:layout_height="2dp"
        android:background="@color/cardview_dark_background"
        app:layout_constraintBottom_toBottomOf="@+id/view"
        app:layout_constraintEnd_toStartOf="@+id/button4"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="@+id/view" />

    <View
        android:id="@+id/view5"
        android:layout_width="0dp"
        android:layout_height="2dp"
        android:background="@color/cardview_dark_background"
        app:layout_constraintBottom_toBottomOf="@+id/view"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button4"
        app:layout_constraintTop_toTopOf="@+id/view" />

</androidx.constraintlayout.widget.ConstraintLayout>


现在,只需将按钮替换为自定义视图,即可完成操作。

您可以使用它来管理步骤状态,而不是手动操作

使用以下代码。只需将Imageview替换为自定义视图

<?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="@dimen/dp75"
    android:layout_marginHorizontal="@dimen/dp4"
    android:gravity="center"
    android:weightSum="7">
    
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/blank_profile_picture"/>
    
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginHorizontal="@dimen/dp4"
        android:layout_weight="1"
        android:background="@color/black"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/blank_profile_picture"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginHorizontal="@dimen/dp4"
        android:layout_weight="1"
        android:background="@color/black"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/blank_profile_picture"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginHorizontal="@dimen/dp4"
        android:layout_weight="1"
        android:background="@color/black"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/blank_profile_picture"/>
        
</LinearLayout>


您可以使用它来管理步骤状态,而不是手动操作

使用以下代码。只需将Imageview替换为自定义视图

<?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="@dimen/dp75"
    android:layout_marginHorizontal="@dimen/dp4"
    android:gravity="center"
    android:weightSum="7">
    
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/blank_profile_picture"/>
    
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginHorizontal="@dimen/dp4"
        android:layout_weight="1"
        android:background="@color/black"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/blank_profile_picture"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginHorizontal="@dimen/dp4"
        android:layout_weight="1"
        android:background="@color/black"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/blank_profile_picture"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginHorizontal="@dimen/dp4"
        android:layout_weight="1"
        android:background="@color/black"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/blank_profile_picture"/>
        
</LinearLayout>


共享xml文件0dp 1dp 0.8@color/step\u line\u gray@dimen/margin\u 2中心上载错误共享xml文件0dp 1dp 0.8@color/step\u line\u gray@dimen/margin\u 2中心上载错误