【问题标题】:Text wrapping in Constraint layout约束布局中的文本换行
【发布时间】:2018-02-28 18:40:41
【问题描述】:

尝试使用 ConstraintLayout 为设置页面创建一个非常简单的布局。 左侧的简单文本视图位于另一个下方,右侧的切换到中心。 我的布局适用于较新的设备,但一旦我切换到 Nexus 4 或更旧的设备,开关就会低于/从视图中消失。

这是我的布局代码,

<android.support.constraint.ConstraintLayout
    android:id="@+id/locationLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="parent">

<TextView
    android:id="@+id/locationTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="@string/location_title"
    android:textStyle="bold"
    app:layout_constraintEnd_toStartOf="@id/guidelineLocationLayout"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:textSize="16sp"/>

<TextView
    android:id="@+id/locationDescription"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/location_description"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/locationTitle"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="@id/locationTitle"
    app:layout_constraintRight_toLeftOf="@id/guidelineLocationLayout"
    android:textSize="12sp"
    />

<Switch
    android:id="@+id/locationPermissionSwitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@id/guidelineLocationLayout"
    app:layout_constraintTop_toBottomOf="parent"
    app:layout_constraintBottom_toTopOf="parent"/>

<View
    android:id="@+id/viewLocationLayout"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#d6d6d6"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/locationDescription"/>

<android.support.constraint.Guideline
    android:id="@+id/guidelineLocationLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_end="48dp" />


</android.support.constraint.ConstraintLayout>

以下是 Pixel 2XL 的约束情况,

这是它们在较小设备上的外观,

我在长描述中使用了 0dp 宽度并定义了左右约束,我还能做什么?

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    您可以使用障碍而不是指南,因为障碍引用多个小部件作为输入,并根据指定一侧的最极端小部件创建虚拟指南。

    根据Documentation

    类似于指南,障碍是一条看不见的线,你可以 将视图限制为。除非障碍没有定义自己的位置; 相反,屏障位置会根据视图的位置移动 包含在其中。当您想要约束视图时,这很有用 到一组视图而不是一个特定的视图。

    希望这对您有所帮助。我使用了屏障而不是指南。

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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:id="@+id/locationLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="parent">
    
        <TextView
            android:id="@+id/locationTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="Save location to the song"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/locationDescription"
            android:layout_width="261dp"
            android:layout_height="32dp"
            android:text="Your location is only requested when a new song is identified. Location details are never sent to the server"
            android:textSize="12sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/locationTitle" />
    
        <android.support.constraint.Barrier
            android:id="@+id/barrier"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:barrierDirection="end"
            app:constraint_referenced_ids="locationDescription,locationTitle" />
    
        <Switch
            android:id="@+id/locationPermissionSwitch"
            android:layout_width="41dp"
            android:layout_height="26dp"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/barrier"
            app:layout_constraintTop_toTopOf="parent" />
    
        <View
            android:id="@+id/viewLocationLayout"
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:background="#d6d6d6"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/locationDescription" />
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      【解决方案2】:

      你需要像这样改变开关的约束

      <Switch
          android:id="@+id/locationPermissionSwitch"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="@id/guidelineLocationLayout"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintBottom_toBottomOf="parent"/>
      

      您将开关的底部限制在父级的顶部,并将开关的顶部限制在父级的底部。您必须将其更改为 layout_constraintTop_toTopOflayout_constraintBottom_toBottomOf

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-12
        • 1970-01-01
        • 2017-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多