今天学习安卓第一行代码时,用gravity=“center”把textview中的内容居中显示,结果误打成了layout_gravity=“center”,运行了一下,发现文字位置没有发生变化。
  论坛查了下,发现两者天差地别。
  gravity是决定控件的内容如何在控件内定位。
  layout_gravity是决定控件如何在其父布局内定位。
  附图

解决方案 »

  1.   

     gravity,自居中,控件中的内容会以当前控件为母体居中显示,
    layout_gravity该控件相对于父布局的位置,当然父布局如果设置居中,就得先以子布局的位置优先,
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_12"
            android:text="ddddd"
            android:gravity="center"
            android:layout_gravity="bottom"/></LinearLayout>,不是所有的布局都有效,RelativeLayout、FrameLayout,都是根据相对位置,或者帧压上去的,具体问题