简体   繁体   中英

constraintlayout baseline to baseline with autosize

I'm trying to align 2 textviews by baseline and use autosize for textsize at the same time. Here is simplified code example. First textview has big size, so textsize auto set to some big value. Second textview smaller than first one so it has smaller auto determined textsize.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/text1"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:text="text1"
        android:maxLines="1"
        android:autoSizeTextType="uniform"
        android:autoSizeMaxTextSize="200sp"
        android:autoSizeMinTextSize="2sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/text2"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/text2"
        android:layout_width="50dp"
        android:layout_height="20dp"
        android:text="text2"
        android:maxLines="1"
        android:autoSizeTextType="uniform"
        android:autoSizeMaxTextSize="200sp"
        android:autoSizeMinTextSize="2sp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/text1"
        app:layout_constraintBaseline_toBaselineOf="@id/text1"/>

</android.support.constraint.ConstraintLayout>

result of example code

The problem is that seems layout_constraintBaseline_toBaselineOf stops working as soon as I use autoSizeTextType

It would be nice if someone can point me right direction. Am I understand wrong how to use constraintlayout? Or is it just baseline_tobaseline does not work with autosize?

I'm using hack for this issue.

Luckily in my case I have two words, so finally I've came up with something like:

private fun getCorrectTextValue(value: Number): SpannableString {
    val currency = getString(R.string.common_currency)
    val formatter = NumberFormat.getInstance(Locale.getDefault())
    val ss = SpannableString("${formatter.format(value)} $currency")
    ss.setSpan(AbsoluteSizeSpan(resources.getDimensionPixelSize(R.dimen.max_text_size)), value.toString().length, value.toString().length + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    return ss
}

It's working for me

Probably you can add an whitespace character using SpannableString and set it's height with:

ss.setSpan(AbsoluteSizeSpan(resources.getDimensionPixelSize(R.dimen.max_text_size)), value.toString().length, value.toString().length + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

I fixed it as follows:

Kotlin :

text1.text = "This is text text text text"
text1.post{

text1.requestLayout()

}

Try removing the autoSize from textview and set the TextView text size. Things will start to work out by themselves. At least this worked for me.

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="56dp"
        android:maxLines="1"
        android:text="text1"
        android:textSize="100sp"
        app:layout_constraintEnd_toStartOf="@+id/text2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/text2"
        android:layout_width="50dp"
        android:layout_height="20dp"
        android:text="text2"
        android:maxLines="1"
        android:autoSizeTextType="uniform"
        android:autoSizeMaxTextSize="200sp"
        android:autoSizeMinTextSize="2sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/text1"
        app:layout_constraintBaseline_toBaselineOf="@id/text1"
    />

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM