【问题标题】:Android ConstraintLayout TextView go off the screenAndroid ConstraintLayout TextView 离开屏幕
【发布时间】:2017-09-21 18:04:41
【问题描述】:

正如您在图片中看到的那样,文字不在墙上。这是一种奇怪的行为,我认为这是一个错误

这是一个屏幕截图:

这是标题 xml:

<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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/windowBackground"
    android:orientation="vertical">

    <TextView
        android:id="@+id/street_name"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginStart="15dp"
        android:text="street name"
        android:textColor="?AppTheme.InformationText"
        android:textSize="14sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_marginTop="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="19dp"
        android:layout_marginLeft="0dp"
        app:layout_constraintWidth_default="wrap"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="@+id/guideline29"
        app:layout_constraintTop_toTopOf="@+id/guideline37"
        app:layout_constraintRight_toLeftOf="@+id/guideline30"
        app:layout_constraintHorizontal_bias="0.605"
        app:layout_constraintVertical_bias="0.0"/>

    <ImageView
        android:id="@+id/street_view_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:padding="6dp"
        android:scaleType="fitCenter"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginLeft="0dp"
        app:layout_constraintLeft_toLeftOf="@+id/country_flag_image"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="0dp"
        app:layout_constraintRight_toLeftOf="@+id/guideline29"
        android:layout_marginRight="8dp"
        app:layout_constraintTop_toTopOf="@+id/guideline37"
        android:layout_marginTop="8dp"/>

    <ImageView
        android:id="@+id/country_flag_image"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginLeft="4dp"
        android:layout_marginStart="4dp"
        android:layout_marginTop="8dp"
        android:adjustViewBounds="true"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintTop_creator="1"
        />

    <de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/exit_image"
        android:layout_width="46dp"
        android:layout_height="46dp"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="-4dp"
        android:src="@drawable/exit_custom"
        app:border_color="#FF000000"
        app:layout_constraintBottom_toBottomOf="@+id/street_view_image"
        app:layout_constraintRight_toRightOf="@+id/street_view_image"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintRight_creator="1"
        />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline29"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5"/>

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline30"
        app:layout_constraintGuide_begin="395dp"
        android:orientation="vertical"/>

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline37"
        app:layout_constraintGuide_begin="8dp"
        android:orientation="horizontal"/>

</android.support.constraint.ConstraintLayout>

我怎样才能使TextView 称为street_name 文本不会出现在屏幕外? 我使用MaterialDrawer,但我认为这与此无关。 设备是具有 Android 6.0.1 的三星 Note 4。在 Android Studio 2.3.3 中使用 ConstraintLayout 版本com.android.support.constraint:constraint-layout:1.0.2

【问题讨论】:

标签: android textview android-constraintlayout


【解决方案1】:

android:layout_width="0dp" 设置为street_name。这将导致它匹配约束并应将其保持在界限内。

不工作? street_name 在右侧被guideline30 约束在395dp395dp 是否在屏幕右侧?

【讨论】:

  • 它不起作用,是的 android:layout_width="0dp" 已设置。你从哪里得到 359dp?
  • @Erik 你检查 guideline30 看它是否离开屏幕了吗?
  • 哇,当我将 guideline30 设置为 procentare 80% 时,它起作用了...谢谢,有这么多的变量..谢谢你陪我走过这个..
【解决方案2】:

添加layconstraintEnd_toEndOf
app:layout_constraintEnd_toEndOf="parent"
和宽度 0dp
android:layout_width="0dp"

单独设置宽度 0dp 是行不通的

【讨论】:

    【解决方案3】:

    使用app:layout_constraintEnd_toEndOf 代替app:layout_constraintRight_toRightOf 也 因为 Right_otRight 在某些情况下可能会显示错误

    并使用Start_toStartOf 而不是Left_toLeft

    也像其他人说的那样设置android:layout_width="0dp" 0dp 表示匹配约束

    【讨论】:

      【解决方案4】:

      对于您的&lt;TextView android:id="@+id/street_name"...&gt;,您应该使用android:layout_width="0dp"android:layout_height="wrap_content",使其看起来像:

      <TextView
              android:id="@+id/street_name"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_marginStart="15dp"
              android:text="street name"
              android:textColor="?AppTheme.InformationText"
              android:textSize="14sp"
              android:textStyle="bold"
              android:gravity="center"
              android:layout_marginTop="8dp"
              android:layout_marginRight="8dp"
              android:layout_marginBottom="19dp"
              android:layout_marginLeft="0dp"
              app:layout_constraintWidth_default="wrap"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintLeft_toLeftOf="@+id/guideline29"
              app:layout_constraintTop_toTopOf="@+id/guideline37"
              app:layout_constraintRight_toLeftOf="@+id/guideline30"
              app:layout_constraintHorizontal_bias="0.605"
              app:layout_constraintVertical_bias="0.0"/>
      

      编辑#1:我想您应该通过父项而不是准则来限制 TextView 的右侧,即将app:layout_constraintRight_toLeftOf="@+id/guideline30" 更改为app:layout_constraintRight_toRightOf="parent"。结果是:

      <TextView
              android:id="@+id/street_name"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_marginStart="15dp"
              android:text="street name"
              android:textColor="?AppTheme.InformationText"
              android:textSize="14sp"
              android:textStyle="bold"
              android:gravity="center"
              android:layout_marginTop="8dp"
              android:layout_marginRight="8dp"
              android:layout_marginBottom="19dp"
              android:layout_marginLeft="0dp"
              app:layout_constraintWidth_default="wrap"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintLeft_toLeftOf="@+id/guideline29"
              app:layout_constraintTop_toTopOf="@+id/guideline37"
              app:layout_constraintRight_toRightOf="parent"
              app:layout_constraintHorizontal_bias="0.605"
              app:layout_constraintVertical_bias="0.0"/>
      

      【讨论】:

      • 不工作。想了一会儿,android:gravity="center" 是小丑,但不是
      【解决方案5】:

      只需将 TextView 附加到右侧就可以了。当您使用 constraintLayout 更好的做法将视图附加到另一个视图或父级时,应使用所有 4 个约束。

      【讨论】:

        猜你喜欢
        • 2021-10-23
        • 1970-01-01
        • 1970-01-01
        • 2018-09-30
        • 2011-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多