【问题标题】:Android AutoResizeTextView - Vertical Gravity off after resizeAndroid AutoResizeTextView - 调整大小后关闭垂直重力
【发布时间】:2017-08-17 20:33:51
【问题描述】:

我正在使用此处找到的 AutoResizeTextView 类。 Auto Scale TextView Text to Fit within Bounds 效果很好。但是,我在 RelativeLayout 中有 textview,并且 TextView 上的 layout_centerVertical 属性设置为 true。效果很好,除非 TextView 不适合并自行调整大小。然后文本不再在 RelativeLayout 中垂直居中。

我猜这是因为垂直对齐是在 TextView 调整大小之前执行的。

我该如何解决这个问题?有没有办法在文本调整大小后重新触发 View 的重力对齐?

示例布局

<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:padding="10dp">

     <com.mypackage.android.AutoResizeTextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Some Text Here"
          android:maxLines="1"
          android:ellipsize="end" />

</RelativeLayout>

【问题讨论】:

    标签: android android-layout android-gravity


    【解决方案1】:

    尝试在

    中也触发resizeText()
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // trigger here
    }
    
    //This is called during layout when the size of this view has changed
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // trigger here
    }
    

    【讨论】:

    • 我在 onMeasure 方法中添加了 resize() 调用,即使调整大小后 textview 也没有正确对齐。谢谢!!!!仅供参考 - 我没有在 TextView 上看到 onSizeChanged 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多