Android 中fill_parent与wrap_content的区别

本文详细解释了Android中组件属性“layout_width”和“layout_height”的使用方法。通过对比“wrap_content”与“fill_parent”(现更名为match_parent),阐述了如何根据不同需求调整组件的大小以适应不同的屏幕尺寸。

在Android中,对于组件的属性“layout_width”和“layout_height”,
其值总是设置为“wrap_content”或“fill_parent”。
那么,这两个值有什么不同呢?   请看下面的定义:  
1. wrap_content:组件的大小以能装入其内容即可; 
  2. fill_parent:组件会显示得和其父组件一样大,并填充剩余的空间(在 API Level 8中命名为 match_parent)。

eg:
1.设置为warp_parent
 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
   
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="FINISH"/>
放置一个Button组件,并设置其宽度和高度为wrap_parent,这会告诉Android将按钮显示为能够装下其内容。
Android <wbr>中fill_parent与wrap_content的区别


2.设置宽为fill_parent
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
   
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="FINISH"/>
将layout_width的值改为fill_parent,现在按钮的宽度填充了剩余的空间,与其父组件Textview的宽度一样,单高度依然是保持在紧紧能容纳下内容。
Android <wbr>中fill_parent与wrap_content的区别

3.设置高度为fill_parent
 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
   
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="FINISH"/>
将layout_width的值改为fill_parent,现在按钮的宽度填充了剩余的空间,与其父组件Textview的宽度一样,单高度依然是保持在紧紧能容纳下内容。
Android <wbr>中fill_parent与wrap_content的区别

4.设置为fill_parent
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
   
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="FINISH"/>
设置其宽度和高度为wrap_parent,这会告诉Android将按钮显示为与整个屏幕一样大,它将填充整个屏幕空间
Android <wbr>中fill_parent与wrap_content的区别
实际上,我们可以指定确切的宽度和高度,不过不建议这样做,因为Android有多种设备屏幕尺寸。我们不知道我们的应用程序会跑在哪一种尺寸的Android设备上。。。。
<?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"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/keypad_test_summary" > </TextView> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/keypad_first" > </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/keypad_second" android:layout_marginTop="4dp" > </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/keypad_third" android:layout_marginTop="4dp" > </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/keypad_fourth" android:layout_marginTop="4dp" > </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/keypad_fifth" android:layout_marginTop="4dp" > </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/keypad_sixth" android:layout_marginTop="4dp" > </LinearLayout> </LinearLayout> </RelativeLayout>
08-16
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入用户名" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName:" > <requestFocus /> </EditText> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入用户密码:" /> </TableRow> <TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content" android:layout_height="wrap_content" > <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="numberPassword" /> </TableRow> <TableRow android:id="@+id/tableRow5" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入出生日期:" /> </TableRow> <EditText android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="numberPassword" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入用户的手机号码:" /> <EditText android:id="@+id/editText4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@/img04.jpg" android:ems="10" android:inputType="phone" /> </TableLayout> </FrameLayout>
最新发布
09-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值