Textview Gravity not working properly in android

后端 未结 5 829
没有蜡笔的小新
没有蜡笔的小新 2021-02-13 18:44

What\'s wrong with my code, I\'m trying to display my TextView named \"invalid\", in different locations (left,right,center), but the gravity (left,right,center) w

相关标签:
5条回答
  • 2021-02-13 19:21

    You have given your TextView width wrap_content, and that is the problem, Check below code and replace it to your code.

    <TextView
    android:id="@+id/txtInvalid"
    android:layout_width="match_parent"   
    android:layout_height="wrap_content"
    android:text="@string/invalid"
    android:gravity="center"   
    />
    
    0 讨论(0)
  • 2021-02-13 19:23

    set android:layout_width="fill_parent" for textView1

    0 讨论(0)
  • 2021-02-13 19:29

    You set this text view a width of "wrap_content" it means, what ever the text is, the view take the size of the text.

    and in the LinearLayout , the default gravity (used here) is 'center'

    you should try this :

    <TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"   <= change this
    android:layout_height="wrap_content"
    android:text="@string/invalid"
    android:gravity="center"   <= then this gravity will be taken into account
    />
    
    0 讨论(0)
  • 2021-02-13 19:35

    Make sure you don't have something like that hanging around

    app:layout_box="left|top"
    
    0 讨论(0)
  • 2021-02-13 19:46

    99% of the time, not working properly == not used properly.

    You are mistaking gravity and layout_gravity.

    gravity is the way the text will align itself in the TextView. The TextView being in wrap_content this does nothing, as the TextView is exactly the size of the text.

    layout_gravity is the way the TextView will align itself in its parent, in your case in the vertical LinearLayout

    0 讨论(0)
提交回复
热议问题