操作方法
我们看这段代码 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageButton0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/help" > </ImageView> <ImageView android:id="@+id/imageButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/imageButton0" android:src="@drawable/c1" > </ImageView> </RelativeLayout>
去掉之前android:layout_centerInParent="true", 我们跑一下 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageButton0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/help" > </ImageView> <ImageView android:id="@+id/imageButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/imageButton0" android:src="@drawable/c1" > </ImageView> </RelativeLayout> 这个程序 会看到我左边图片样子 帮助按钮会在古天乐(c1)的上方
然后我们加上代码android:layout_centerInParent="true" 这时我们会发现 图片都去中央去了 这说明这个属性有居中的功能但我们还是不能理解父控件是谁 是古天乐图片控件 还是相对布局
这时我们把古天乐控件去掉 我们在看效果: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageButton0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/help" > </ImageView> </RelativeLayout> 我们会发现依然在原来的位置上没变,这说明是相对整个布局而居中的 这个·父控件就是RelativeLayout