Align top two textviews with different font sizes

假如想象 提交于 2019-12-11 01:18:14

问题


Im using constraint layout and i want to achieve the following:

The app:layout_constraintBaseline_toBaselineOf attribute bottom aligns the two textviews, is there any way to top align the two? The regular app:layout_constraintTop_toTopOf is not working of course because of the size differences.


回答1:


So it seems there is no a convenient way of doing this and a custom view must be implemented. I've taken inspiration from here:

https://github.com/fabiomsr/MoneyTextView




回答2:


Try this .

<?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="wrap_content">

    <TextView
        android:id="@+id/tv_unit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:text="$"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:text="120"
        android:textSize="40sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toRightOf="@+id/tv_unit"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

Like this .




回答3:


Do this way

String s = "$<sub>12 </sub>";
        textView.setText(Html.fromHtml(s)); // 12 will cropped 
// solution:
        s = "$<sub>12 </sub>\t  "; // add behind ending of sup tag the tabulator \t,
// but not char \t but only press to TAB key!!! in source code
        textView.setText(Html.fromHtml(s)); // 12 is visible correctly

http://android.okhelp.cz/whittled-superscript-sup-tag-textview-android-issue/




回答4:


try this you can use Html.fromHtml for this purpose

Html.fromHtml Returns displayable styled text from the provided HTML string.

yourTextview.setText(Html.fromHtml("<sup><small>$</small></sup>42"));

or try this use SpannableString

TextView textView = rootView.findViewById(R.id.tv);
SpannableStringBuilder sb = new SpannableStringBuilder("$42");
sb.setSpan(new SuperscriptSpan(), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(sb, TextView.BufferType.SPANNABLE);


来源:https://stackoverflow.com/questions/46275592/align-top-two-textviews-with-different-font-sizes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!