Can´t center textview´s text vertically

前端 未结 7 1576
闹比i
闹比i 2021-02-09 16:20

[Solved] I had to add android:fillViewport=\"true\" to the ScrollView, that fixed the problem with the text not centering vertically.

I know this has been

7条回答
  •  情歌与酒
    2021-02-09 16:45

    for others that have this problem there is a bug in Relative Layouts after api 18 see https://code.google.com/p/android/issues/detail?id=59368

    a fix that worked for me was to replace my relative layout with this code (found in the above link)

    DISCLAIMER : this only worked in one specific situation. It failed horrible in another. Feel free to try it out if you want

    public class FixRelativeLayoutBug extends RelativeLayout {
        public FixRelativeLayoutBug(Context context) {
            super(context);
        }
    
        public FixRelativeLayoutBug(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public FixRelativeLayoutBug(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        private static final int AT_MOST_UNSPECIFIED = MeasureSpec.makeMeasureSpec((1<<30)-1, MeasureSpec.AT_MOST);
    
        @Override
        protected void onMeasure(int widthSpec, int heightSpec)
        {
            // RelativeLayout has bugs when measured in UNSPECIFIED height mode that were supposedly fixed
            // in API 18. However the 'fix' does not work with breaks gravity == center_vertical in TextView.
            // https://code.google.com/p/android/issues/detail?id=63673
            if (MeasureSpec.getMode(heightSpec) == MeasureSpec.UNSPECIFIED) heightSpec = AT_MOST_UNSPECIFIED;
            super.onMeasure(widthSpec, heightSpec);
        }
    }
    

提交回复
热议问题