Android Mobile Phone Development Control TextView Text Center

  • 2021-07-13 06:18:54
  • OfStack

There are two ways to center TextView text:

1: Set in xml file: Android: gravity = "center"

2: Set in the program: txtTitle. setGravity (Gravity. CENTER);

Set the control to center:

android: layout_gravity= "center" is to center the textview control in the overall layout, or you can call the setting android: gravity= "center" in its parent layout

The program also needs to set the parent layout of its control, for example:


RelativeLayout.LayoutParams layoutParams= 
  new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); 
mTextView.setLayoutParams(layoutParams); 

In fact, it is easy to understand that "layout" is the control's operation on the whole layout

Note: Set vertical center, which can be set in XML

Android:gravity="center_vertical"


Related articles: