ConstraintLayout:将textview的顶部与imageview对齐

时间:2017-06-29 22:05:28

标签: android android-constraintlayout

我尝试通过提供

来在约束布局中对齐图像顶部和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"
    />

,但这些视图的顶部是对齐的,而不是内容,因为字体顶部和底部有一些空白区域。有谁知道如何解决这个问题? (问题可以在下面的图片中看到)

enter image description here

3 个答案:

答案 0 :(得分:0)

尝试此技巧:

<?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>

现在,其外观如下图所示:

enter image description here

希望这对您有所帮助。编码愉快。

答案 1 :(得分:0)

您可以通过添加

android:padding|start|Top|End|Bottom="XXdp"

填充可在视图周围创建空白区域,基本上可以创建所需的效果。

请注意,这会使图片变小,特别是如果将width / height设置为一个值。

例如现在,图像视图具有32dp->该图像有32dp的空间。 如果添加填充(例如4dp),则最终将获得32dp视图,但实际图像将具有24dp(4 + 24 + 4)Handy visual representation

答案 2 :(得分:0)

您将不得不为txt_medal_title设置一个负的页边距顶部,以使其稍微向屏幕上方移动。

您可以通过2种方式设置此保证金的金额。

  • 准确:您必须在代码中执行此操作。将如下所示:
TextView medalTitle = findViewById(R.id.txt_medal_title);
MarginLayoutParams params = (MarginLayoutParams) medalTitle.getLayoutParams();
FontMetrics fm = medalTitle.getTextPaint().getFontMetrics();
params.marginTop = (int) (fm.top - fm.ascent);
  • 外观合适:您在布局中使用sp单位使用负边距值。您将不得不尝试几次不同的值以获得所需的结果。请注意,每次更改文本大小或使用的字体时,都必须重新检查该值。
相关问题