【layout】fill_parent和wrap_content的区别

本文详细解释了在UI布局中使用fill_parent和wrap_content属性的意义。fill_parent用于让控件扩展并填充其父容器的所有可用空间,而wrap_content则根据控件内部的内容自动调整大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Either attribute can be applied to View's (visual control) horizontal or vertical size. It's used to set a View or Layouts size based on either it's contents or the size of it's parent layout rather than explicitly specifying a dimension.

两个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精确地指定视图范围更加方便。


fill_parent


Setting the layout of a widget to fill_parent will force it to expand to take up as much space as is available within the layout element it's been placed in. It's roughly equivalent of setting the dockstyle of a Windows Form Control to Fill.


设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间。这跟Windows控件的dockstyle属性大体一致。


Setting a top level layout or control to fill_parent will force it to take up the whole screen.


设置一个顶部布局或控件为fill_parent将强制性让它布满整个屏幕。


wrap_content

Setting a View's size to wrap_content will force it to expand only far enough to contain the values (or child controls) it contains. For controls -- like text boxes (TextView) or images (ImageView) -- this will wrap the text or image being shown. For layout elements it will resize the layout to fit the controls / layouts added as its children.


设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。以TextView和ImageView控件为例,设置为wrap_content将完整显示其内部的文本和图像。布局元素将根据内容更改大小。




It's roughly the equivalent of setting a Windows Form Control's Autosize property to True.


设置一个视图的尺寸为wrap_content大体等同于设置Windows控件的Autosize属性为True。




简言之:fill_parent布局指将视图(在Windows中称为控件)扩展以填充所在容器的全部空间。


        wrap_content布局指根据视图内部内容自动扩展以适应其大小。


 
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@id/btn_test_helloworld" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10.0dip" android:text="动态模板测试" android:layout_alignParentTop="true" /> <Button android:id="@id/btn_test_zixun" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10.0dip" android:text="静态资源测试" android:layout_below="@id/btn_test_helloworld" /> <Button android:id="@id/btn_test_multibundle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10.0dip" android:text="多bundle测试" android:layout_below="@id/btn_test_zixun" /> <Button android:id="@id/btn_test_invokemodule" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10.0dip" android:text="测试scheme调起" android:layout_below="@id/btn_test_multibundle" /> <Button android:id="@id/btn_test_performance" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10.0dip" android:text="性能测试入口" android:layout_below="@id/btn_test_invokemodule" /> <Button android:id="@id/btn_enter_dev" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10.0dip" android:text="打开Debug设置页面" android:layout_below="@id/btn_test_performance" /> </RelativeLayout>。以上代码 添加什么可以实现 app界面 镜面翻转显示
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值