ConstraintLayout 将 TextView 弹出屏幕
ConstraintLayout Bumping TextView Off Screen
我有一个包含 ImageView 和 TextView 的 XML 文件。每次填充 TextView 时,文本都会从屏幕上消失。我查看了遇到同样问题的其他人,常见的解决方案似乎是将 Textview 的宽度设置为 0dp。这对我没有用。想知道有没有人知道为什么?
这是我的 constraintLayout XML 文件:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/weather_icon"
app:layout_constraintRight_toLeftOf="@id/weather_data"
android:layout_width="50dp"
android:layout_height="match_parent" />
<TextView
android:id="@+id/weather_data"
android:text="EXAMPLE"
android:textStyle="bold"
android:paddingRight="16dp"
style="@style/TextAppearance.AppCompat.Large"
app:layout_constraintLeft_toRightOf="@id/weather_icon"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
这是它的样子,文本明显被截断了:
我得到了你的问题的解决方案,请使用它:
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/weather_icon"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/weather_data"
android:layout_width="50dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/weather_data"
android:text="EXAMPLE "
android:textStyle="bold"
android:paddingRight="16dp"
style="@style/TextAppearance.AppCompat.Large"
app:layout_constraintLeft_toRightOf="@id/weather_icon"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>
您应该将 TextView
的宽度设置为 match_constraint
,而在 XML 中则为 0dp
。
我看到您同时使用了 left/right 和 start/end 符号。只坚持其中一个。
我鼓励您使用 start/end 表示法,因为可能有使用您应用的设备的语言是从右到左阅读的语言,即:阿拉伯语,因此使用 start/end 您不用担心这些设备。
感谢 Sandeep 和 Martin。我结合了他们的建议,这在布局中效果很好:
<ImageView
android:id="@+id/weather_icon"
app:layout_constraintEnd_toStartOf="@id/weather_data"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="50dp"
android:layout_height="wrapcontent" />
<TextView
android:id="@+id/weather_data"
android:text="EXAMPLE"
android:textStyle="bold"
android:paddingRight="16dp"
app:layout_constraintStart_toEndOf="@id/weather_icon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="@style/TextAppearance.AppCompat.Large"
android:layout_width="0dp"
android:layout_height="wrap_content" />
我有一个包含 ImageView 和 TextView 的 XML 文件。每次填充 TextView 时,文本都会从屏幕上消失。我查看了遇到同样问题的其他人,常见的解决方案似乎是将 Textview 的宽度设置为 0dp。这对我没有用。想知道有没有人知道为什么?
这是我的 constraintLayout XML 文件:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/weather_icon"
app:layout_constraintRight_toLeftOf="@id/weather_data"
android:layout_width="50dp"
android:layout_height="match_parent" />
<TextView
android:id="@+id/weather_data"
android:text="EXAMPLE"
android:textStyle="bold"
android:paddingRight="16dp"
style="@style/TextAppearance.AppCompat.Large"
app:layout_constraintLeft_toRightOf="@id/weather_icon"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
这是它的样子,文本明显被截断了:
我得到了你的问题的解决方案,请使用它:
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/weather_icon"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/weather_data"
android:layout_width="50dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/weather_data"
android:text="EXAMPLE "
android:textStyle="bold"
android:paddingRight="16dp"
style="@style/TextAppearance.AppCompat.Large"
app:layout_constraintLeft_toRightOf="@id/weather_icon"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>
您应该将 TextView
的宽度设置为 match_constraint
,而在 XML 中则为 0dp
。
我看到您同时使用了 left/right 和 start/end 符号。只坚持其中一个。
我鼓励您使用 start/end 表示法,因为可能有使用您应用的设备的语言是从右到左阅读的语言,即:阿拉伯语,因此使用 start/end 您不用担心这些设备。
感谢 Sandeep 和 Martin。我结合了他们的建议,这在布局中效果很好:
<ImageView
android:id="@+id/weather_icon"
app:layout_constraintEnd_toStartOf="@id/weather_data"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="50dp"
android:layout_height="wrapcontent" />
<TextView
android:id="@+id/weather_data"
android:text="EXAMPLE"
android:textStyle="bold"
android:paddingRight="16dp"
app:layout_constraintStart_toEndOf="@id/weather_icon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="@style/TextAppearance.AppCompat.Large"
android:layout_width="0dp"
android:layout_height="wrap_content" />