![](/img/trans.png)
[英]Align ImageView with TextView in a ConstraintLayout
[英]ConstraintLayout: align top of textview with imageview
我试图通过提供在约束布局中对齐图像和文本视图的顶部
<ImageView
android:id="@+id/img_medal"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:contentDescription="@string/default_content_description"
android:src="@drawable/medal_gold"
app:layout_constraintLeft_toLeftOf="@+id/view_award_region"
app:layout_constraintTop_toTopOf="@+id/view_award_region" />
<TextView
android:id="@+id/txt_medal_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="0dp"
android:text="You are gold now."
app:layout_constraintLeft_toRightOf="@+id/img_medal"
app:layout_constraintTop_toTopOf="@+id/img_medal"
style="@style/SettingsMedalTitle"
/>
,但这些视图的顶部对齐,而不是内容,因为字体的顶部和底部有一些空白。 有谁知道如何解决这个问题? (问题见下图)
试试这个黑客:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img_medal"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:paddingTop="6dp"
android:contentDescription="@string/app_name"
android:src="@android:drawable/sym_def_app_icon"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_medal_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="You are gold now."
android:textSize="20sp"
app:layout_constraintStart_toEndOf="@+id/img_medal"
app:layout_constraintTop_toTopOf="@+id/img_medal" />
</android.support.constraint.ConstraintLayout>
现在,它将如下图所示:
希望这对你有帮助。 快乐编码。
您必须为txt_medal_title
设置一个负边距顶部,以使其向屏幕上方移动一点。
您可以通过两种方式设置此保证金的金额。
TextView medalTitle = findViewById(R.id.txt_medal_title);
MarginLayoutParams params = (MarginLayoutParams) medalTitle.getLayoutParams();
FontMetrics fm = medalTitle.getPaint().getFontMetrics();
params.topMargin = (int) (fm.top - fm.ascent);
sp
单位使用负边距值。 您必须多次尝试不同的值才能获得所需的结果。 请注意,每次更改文本大小或使用的字体时,都必须重新检查此值。我试图通过提供对齐方式在约束布局中对齐图像和textview的顶部
<ImageView
android:id="@+id/img_medal"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:contentDescription="@string/default_content_description"
android:src="@drawable/medal_gold"
app:layout_constraintLeft_toLeftOf="@+id/view_award_region"
app:layout_constraintTop_toTopOf="@+id/view_award_region" />
<TextView
android:id="@+id/txt_medal_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="0dp"
android:text="You are gold now."
app:layout_constraintLeft_toRightOf="@+id/img_medal"
app:layout_constraintTop_toTopOf="@+id/img_medal"
style="@style/SettingsMedalTitle"
/>
,但是这些视图的顶部对齐,而不是内容对齐,因为字体的顶部和底部有一些空白。 有谁知道如何解决这个问题? (问题可以在下面的图片中看到)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.