【问题标题】:Calculate the size of the text based on the height of the TextView根据TextView的高度计算文本的大小
【发布时间】:2020-07-27 09:01:28
【问题描述】:

我在 Horizo​​ntalScrollView 中有一个 TextView,如下所示。目前,我使用的是固定的文本大小。现在,我想计算在文本大小不超过 TextView 高度的情况下可以使用的最大文本大小。请注意,文本的宽度不是问题,因为 TextView 是 Horizo​​ntalScrollView 的子视图,因此文本一旦扩展了允许的宽度,就会变得可滚动。

<HorizontalScrollView
    android:id="@+id/horizontal_scroll_view_equation"
    android:scrollbars="none"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="0dp"
    android:fillViewport="true"
    android:layout_gravity="right">

    <LinearLayout
        android:layout_gravity="right"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_marginRight="@dimen/spacing_medium"
            android:layout_marginLeft="@dimen/spacing_medium"
            android:id="@+id/text_view_equation"
            android:background="@android:color/transparent"
            android:gravity="center_vertical|right"
            android:layout_gravity="right"
            android:maxLines="1"
            android:textSize="@dimen/font_xxxlarge"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </LinearLayout>
</HorizontalScrollView>

我一直在查看 StackOverflow 和其他论坛上的帖子,但我只能找到同时考虑宽度和高度的帖子。

有没有简单的解决方案可以根据TextView的高度计算最大文本大小?

【问题讨论】:

    标签: android textview horizontalscrollview android-textview-autosize


    【解决方案1】:

    其实我自己找到了答案。解决方法如下。首先,我做了一个自定义的TextView,如下图。

    import android.content.Context;
    import android.util.AttributeSet;
    
    import androidx.appcompat.widget.AppCompatTextView;
    
    
    public class AutoSizeTextView extends AppCompatTextView {
    
        public AutoSizeTextView(Context context) {
            super(context);
        }
    
        public AutoSizeTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public AutoSizeTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
    
            this.getPaint().setTextSize(h * 0.75f);
            invalidate();
        }
    }
    

    然后我在Horizo​​ntalScrollView中使用了这个TextView作为child,如下所示。

    <HorizontalScrollView
        android:id="@+id/horizontal_scroll_view_equation"
        android:scrollbars="none"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:fillViewport="true"
        android:layout_gravity="right">
    
        <LinearLayout
            android:layout_gravity="right"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:orientation="vertical">
    
            <com.yourpackage.AutoSizeTextView
                android:layout_marginRight="@dimen/spacing_medium"
                android:layout_marginLeft="@dimen/spacing_medium"
                android:id="@+id/text_view_equation"
                android:background="@android:color/transparent"
                android:gravity="center_vertical|right"
                android:layout_gravity="right"
                android:maxLines="1"
                android:textSize="@dimen/font_xxxlarge"
                android:layout_width="wrap_content"
                android:layout_height="match_parent" />
        </LinearLayout>
    </HorizontalScrollView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2019-05-13
      • 2018-08-01
      • 1970-01-01
      相关资源
      最近更新 更多