【问题标题】:ConstraintLayout: align top of textview with imageviewConstraintLayout:将 textview 的顶部与 imageview 对齐
【发布时间】:2017-12-03 18:42:45
【问题描述】:

我尝试通过提供

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

,但是这些视图的顶部对齐,而不是内容,因为字体的顶部和底部有一些空白空间。有谁知道如何解决这个问题? (问题见下图)

【问题讨论】:

  • 您希望文本以 ImageView 为中心?

标签: android android-constraintlayout


【解决方案1】:

试试这个技巧:

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

现在,如下图所示:

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

【讨论】:

    【解决方案2】:

    您必须为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 单位的布局中使用负边距值。您必须多次尝试不同的值才能获得所需的结果。请注意,每次更改文本大小或使用的字体时,都必须重新检查此值。

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 2013-12-21
      • 1970-01-01
      • 2019-09-20
      相关资源
      最近更新 更多