首页 > 解决方案 > 如何在 ConstraintLayout 布局中设置 textview 水平 70 和 30 百分比

问题描述

<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="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="14dp"
    android:paddingEnd="16dp"
    android:paddingBottom="14dp">

    <ImageView
        android:id="@+id/indicator"
        android:layout_width="8dp"
        android:layout_height="8dp"
        android:layout_marginStart="@dimen/dp_16"
        android:layout_marginTop="6dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@drawable/ic_bullet_point_yellow" />

    <TextView
        android:id="@+id/tv_left"
        style="@style/Tesco__Theme.Text.T3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/dp_16"
        android:textColor="@color/tesco__dove_gray"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/indicator"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="asdasdsasddsadsadsadsaddasewere ewr" />

    <TextView
        android:id="@+id/tv_right"
        style="@style/Tesco__Theme.Text.H6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/dp_16"
        android:layout_marginEnd="@dimen/dp_16"
        android:gravity="end|center_horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tv_left"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="sadsadsdsaasdasdassadsadasZ" />

    <androidx.constraintlayout.helper.widget.Flow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/dp_16"
        android:orientation="horizontal"
        app:constraint_referenced_ids="indicator,tv_left,tv_right"
        app:flow_horizontalAlign="end"
        app:flow_horizontalGap="@dimen/dp_16"
        app:flow_horizontalStyle="spread_inside"
        app:flow_verticalAlign="center"
        app:flow_wrapMode="chain"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是我正在使用 Flow 布局水平对齐所有三个视图的布局我想设置 tv_left 70% 和 tv_right 30% 我已经尝试使用 app:layout_constraintHorizo​​ntal_weight 但它不起作用我不知道我在做什么错误请帮助我在这

标签: android

解决方案


请尝试以下代码。我希望我清楚地理解你的问题。

通过使用约束布局:

 <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="30dp"
            android:src="@android:drawable/ic_menu_add"
            android:layout_height="30dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/textView"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:gravity="center|start"
            android:text="Lorem Ipsum is simply dummy text of the printing and typesettLorem Ipsum is simply dummy text of the printing and typesettLorem Ipsum is simply dummy text of the printing and typesett"
            android:layout_height="match_parent"
            app:layout_constraintEnd_toStartOf="@+id/textView2"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/imageView" />

        <TextView
            android:id="@+id/textView2"
            android:gravity="center|start"
            android:text="Lorem Ipsum is simply dummy text of the printing and typesett"
            android:layout_width="60dp"
            android:layout_height="match_parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/textView" />

    </androidx.constraintlayout.widget.ConstraintLayout>

通过使用 LinearLayout 权重

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:weightSum="2">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.3">
            <ImageView
                android:layout_width="20dp"
                android:src="@android:drawable/ic_menu_add"
                android:layout_centerInParent="true"
                android:layout_height="20dp"/>
        </RelativeLayout>

        <TextView
            android:layout_width="0dp"
            android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
            android:layout_height="match_parent"
            android:layout_weight="1.1" />

        <TextView
            android:layout_width="0dp"
            android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
            android:layout_height="match_parent"
            android:layout_weight="0.6" />
    </LinearLayout>

推荐阅读