I tried to align top of image and textview in constraint layout by providing
<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"
/>
, but the tops of these views get aligned, and not the content, since there is some empty space on the top and bottom of font. Does anyone know how to solve this problem? (Problem can be seen on the picture below)
Try this hack:
<?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>
Now, it would look as in the image below:
Hope this helps you. Happy coding.
You will have to set a negative margin top for txt_medal_title
to make it move a bit towards upside of the screen.
There are 2 ways in which you can set the amount of this margin.
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
units. You will have to try different values a few times to get the result you want. Note that you will have to recheck this value every time you change the text size or the font that you have used.I tried to align top of image and textview in constraint layout by providing
<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"
/>
, but the tops of these views get aligned, and not the content, since there is some empty space on the top and bottom of font. Does anyone know how to solve this problem? (Problem can be seen on the picture below)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.