【问题标题】:Align top two textviews with different font sizes用不同的字体大小对齐前两个文本视图
【发布时间】:2017-09-18 20:27:15
【问题描述】:

我正在使用约束布局,我想实现以下目标:

app:layout_constraintBaseline_toBaselineOf 属性底部对齐两个文本视图,有没有办法顶部对齐两个?由于大小差异,常规 app:layout_constraintTop_toTopOf 当然无法正常工作。

【问题讨论】:

标签: android android-constraintlayout


【解决方案1】:

所以似乎没有一种方便的方法可以做到这一点,必须实现自定义视图。我从这里获得了灵感:

https://github.com/fabiomsr/MoneyTextView

【讨论】:

    【解决方案2】:

    试试这个。

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

    像这样。

    【讨论】:

    • 这更容易..great +1
    • 这很简单。而且ConstraintLayout 类似于RelativeLayout
    • 这不是一个合适的解决方案,您只是使用硬编码的边距来对齐两个视图。
    • 你想为它做什么?
    【解决方案3】:

    这样做

    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】:

      试试这个 为此,您可以使用 Html.fromHtml

      Html.fromHtml 从提供的 HTML 字符串返回可显示的样式文本。

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

      或尝试使用 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);
      

      【讨论】:

      • 没有得到想要的效果
      • 不,这两个文本没有完全对齐。美元符号高于数字。
      • @EvgeniRoitburg 使用 SpannableString
      猜你喜欢
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 2020-09-06
      • 2021-09-20
      • 1970-01-01
      • 2015-08-07
      • 1970-01-01
      • 2013-10-25
      相关资源
      最近更新 更多