Android Wrap_content And Fill_parent Example

来源:互联网 发布:淘宝里在哪里查看评价 编辑:程序博客网 时间:2024/06/10 23:17

Android Wrap_content And Fill_parent Example

在 Android 中,经常对组件属性 “layout_width” 和“layout_height“ 使用  “wrap_content” 或 “fill_parent”  ,那么,“wrap_content” 与“fill_parent” 的区别是什么呢?


看下面的定义 :


wrap_content – The component just want to display big enough to enclose its content only.
fill_parent – The component want to display as big as its parent, and fill in the remaining spaces. (renamed match_parent in API Level 8)




上面的定义太罗嗦了,还是看例子吧:



1. wrap_content

将Button按钮组件的 width 和 height 属性的值设置为“wrap_content”。It tell Android to display the button big enough to enclose it’s content “Button ABC” only.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    tools:context=".MainActivity" >    <Button        android:id="@+id/btnButton1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button ABC" /></RelativeLayout>





2. fill_parent – width
Change the “layout_width” to “fill_parent“, now, the button’s width will fill in the remaining spaces, just as big as it’s parent “RelativeLayout“, but button’s height is still big enough to enclose it’s content only.

将 “layout_width”的值改为“fill_parent“,现在呢,查看结果,button按钮的宽度已经变了,button按钮 填充剩余空白,正如它的父级RelativeLayout

但是,button按钮的高度没有改变,仍然是按钮尽可能大的包裹它的文本内容。    


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    tools:context=".MainActivity" >    <Button        android:id="@+id/btnButton1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Button ABC" /></RelativeLayout>





3. fill_parent – height
Change the “layout_height” to “fill_parent“, now, the button’s height will fill in the remaining spaces, just as big as it’s parent “RelativeLayout“, but button’s width is still big enough to enclose it’s content only.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    tools:context=".MainActivity" >    <Button        android:id="@+id/btnButton1"        android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:text="Button ABC" /></RelativeLayout>




4. fill_parent – width, height
Change the both “layout_width” and “layout_height” to “fill_parent“, the button will display as big as the whole device screen, it just fill in the entire screen space.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    tools:context=".MainActivity" >    <Button        android:id="@+id/btnButton1"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:text="Button ABC" /></RelativeLayout>



参考文献:

http://www.mkyong.com/android/android-wrap_content-and-fill_parent-example/

=============================

再看一个例子,就能理解 “wrap包裹”的意思了:

button.setText("hello"+"\n"+"beijing"+"\n"+"android");



原创粉丝点击