简体   繁体   中英

Text in TextView gravity not working

I written the following function to create text view.But some the text not visible completely so i get gravity to center_horizontal|center|vertical but it is not working.I the following images IN DATE & OUT DATE not visible completely.So i want to set all text to center vertical and center horizontal

protected TextView createDefaultTabView(Context context) {
        TextView textView = new TextView(context);
        textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
        textView.setTypeface(Typeface.DEFAULT_BOLD);
        //textView.setPadding(0,0,0,25);

        textView.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    [![enter image description here][1]][1]    TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
                outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
        textView.setAllCaps(true);

        int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
        textView.setPadding(padding, padding, padding, padding + 10);

        return textView;
    }

在此输入图像描述

Your TextView's height is set to WRAP_CONTENT , so it's internal Gravity has no effect on it's vertical positioning (because there's no extra bounds to position itself in).

If you want to get it vertically aligned, you need to either set the TextView's PARENT gravity to CENTER_VERTICAL , or set the TextView's height to MATCH_PARENT .

Either of those will give you the effect you seek.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM