如何区分Android wrap_content和fill_parent的详细说明

在Android中,总是把“wrap_content“或”fill_parent“组件属性”layout_width“和”layout_height“搞混?

看看下面的定义:

wrap_content–组件只想显示大到足以包围其内容只。
fill_parent–组件想要显示为大,填补剩余的空间。(后改名为match_parent在API级别8)
让我们看看下面的演示:

1。wrap_content
一个按钮元件,设置”wrap_content“在宽度和高度属性。它告诉Android显示按钮足够大,把它的内容”按钮ABC“只有。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/btnButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button ABC"/>

</RelativeLayout>

image.png

2。fill_parent–宽度
改变“layout_width“”fill_parent“,按钮的宽度将填补剩余的空间,因为”一样大相对布局”按钮的高度,但仍足以把它的内容。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/btnButton1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Button ABC"/>

</RelativeLayout>

image.png

3.fill_parent–高度
改变“layout_height“”fill_parent“,按钮的高度将填补剩余的空间,因为”一样大相对布局”按钮的宽度,但仍足以把它的内容。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/btnButton1"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:text="Button ABC"/>

</RelativeLayout>

image.png

4。fill_parent–宽度、高度
改变“layout_width“和”layout_height“”fill_parent”按钮将显示为整个设备的屏幕一样大,它只填满整个屏幕空间。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/btnButton1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="Button ABC"/>

</RelativeLayout>

image.png

原文博客地址:http://www.apkbus.com/blog-919651-76525.html
事实上,你可以指定一个确切的宽度和高度,但这是不推荐的,由于Android设备的各种屏幕尺寸。你只是不知道什么尺寸的Android设备上运行你的幻想中的应用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值