将视图底部与文本基线对齐
Align view bottom to text baseline
在 ConstraintLayout 中,有没有办法将 View(例如 ImageView)的底部与 TextView 的基线对齐?我希望有像 app:layout_constraintBottom_toBaselineOf
这样的约束,但不存在这样的约束。
注意:我试过 app:layout_constraintBaseline_toBaselineOf
,但看起来只有在 TextView 上定义时才有效。
来自developer guidelines for ConstraintLayout
:
baselines can constrain only to other baselines
和
Baseline alignment
Align the text baseline of a view to the text baseline of another view.
据此以及我自己的实验,似乎不可能将通用 View
的底部或顶部限制到 TextView
的基线。
这可能为时已晚,但我希望对阅读 post 的人有所帮助。
在此特定示例中,如果您想要将 ImageView
与 TextView
的基线对齐,ImageView
的默认对齐设置将应用于 "top",不知道为什么。很可能你想将它应用到 ImageView
的底部,这可以通过设置 android:baselineAlignBottom="true"
属性来实现。更多信息在这里:https://developer.android.com/reference/android/widget/ImageView.html#attr_android:baselineAlignBottom
所以 ConstraintLayout
的完整代码如下所示:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.AppCompatImageView
android:id="@+id/imageView"
android:layout_width="96dp"
android:layout_height="96dp"
android:baselineAlignBottom="true"
app:layout_constraintBaseline_toBaselineOf="@id/textView"
app:layout_constraintStart_toEndOf="@id/textView"
app:srcCompat="@drawable/drawable1"
android:contentDescription="@null"/>
</android.support.constraint.ConstraintLayout>
我在我的项目中使用 AppCompatImageView
,但我很确定常规 ImageView
的工作方式相同。
通过向 ImageView
添加 layout_alignBaseline="@id/textView"
,RelativeLayout
也可以实现相同的行为。
如果由于某种原因这不起作用,例如您有自定义视图或其他东西,您也可以考虑在运行时进行。
TextView
中有一个方法,叫做getLastBaselineToBottomHeight
。它 returns 最后一个文本基线与此 TextView
底部之间的距离。您可以将该值应用于 View
的底部边距,这应该会产生相同的效果。虽然在 AndroidP 中才引入该方法,但你可以简单地按照相同的方式实现它(根据源代码)。只是一个对我有用的例子:
MarginLayoutParams params = (MarginLayoutParams) rootView.findViewById(R.id.imageView).getLayoutParams();
params.bottomMargin = textView.getPaddingBottom() + textView.getPaint().getFontMetricsInt().descent;
希望对您有所帮助。祝你好运!
在 ConstraintLayout 中,有没有办法将 View(例如 ImageView)的底部与 TextView 的基线对齐?我希望有像 app:layout_constraintBottom_toBaselineOf
这样的约束,但不存在这样的约束。
注意:我试过 app:layout_constraintBaseline_toBaselineOf
,但看起来只有在 TextView 上定义时才有效。
来自developer guidelines for ConstraintLayout
:
baselines can constrain only to other baselines
和
Baseline alignment
Align the text baseline of a view to the text baseline of another view.
据此以及我自己的实验,似乎不可能将通用 View
的底部或顶部限制到 TextView
的基线。
这可能为时已晚,但我希望对阅读 post 的人有所帮助。
在此特定示例中,如果您想要将 ImageView
与 TextView
的基线对齐,ImageView
的默认对齐设置将应用于 "top",不知道为什么。很可能你想将它应用到 ImageView
的底部,这可以通过设置 android:baselineAlignBottom="true"
属性来实现。更多信息在这里:https://developer.android.com/reference/android/widget/ImageView.html#attr_android:baselineAlignBottom
所以 ConstraintLayout
的完整代码如下所示:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.AppCompatImageView
android:id="@+id/imageView"
android:layout_width="96dp"
android:layout_height="96dp"
android:baselineAlignBottom="true"
app:layout_constraintBaseline_toBaselineOf="@id/textView"
app:layout_constraintStart_toEndOf="@id/textView"
app:srcCompat="@drawable/drawable1"
android:contentDescription="@null"/>
</android.support.constraint.ConstraintLayout>
我在我的项目中使用 AppCompatImageView
,但我很确定常规 ImageView
的工作方式相同。
通过向 ImageView
添加 layout_alignBaseline="@id/textView"
,RelativeLayout
也可以实现相同的行为。
如果由于某种原因这不起作用,例如您有自定义视图或其他东西,您也可以考虑在运行时进行。
TextView
中有一个方法,叫做getLastBaselineToBottomHeight
。它 returns 最后一个文本基线与此 TextView
底部之间的距离。您可以将该值应用于 View
的底部边距,这应该会产生相同的效果。虽然在 AndroidP 中才引入该方法,但你可以简单地按照相同的方式实现它(根据源代码)。只是一个对我有用的例子:
MarginLayoutParams params = (MarginLayoutParams) rootView.findViewById(R.id.imageView).getLayoutParams();
params.bottomMargin = textView.getPaddingBottom() + textView.getPaint().getFontMetricsInt().descent;
希望对您有所帮助。祝你好运!