理解android:layout_centerInParent="true"

作者:国际小甜 | 创建时间: 2023-04-28
android:layout_centerInParent="true"这句话定义为将控件置于父控件的中心位置还是不知道什么意思,在这里我来一次咬文嚼字 关键词 控件 父控件 理解这两个概念就好办...
理解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: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

点击展开全文

更多推荐