现在的位置: 首页 > 综合 > 正文

Layout _width ,Layout_height和heigh ,width 区别:

2018年05月19日 ⁄ 综合 ⁄ 共 2246字 ⁄ 字号 评论关闭

Layout,翻译为中文的意思是 布局,安排,版面设计。对于许多的组件的命令,都有Layout_x的区别,而许多的区别很明显,直接是Layout是相对于父容器(一般就是整个xml的布局)的改变,一个是相对于组件本身的改变。比较典型的就是gravity和layout_gravity,当然也有例外的 ,比如Layout_marginpanding

对于每个组件,可以独自的使用Layout _width ,layout_heigh直接的设置相对于父容器的大小,

设置为 wrap_content或者 match_parent. 但是heigh ,width不能这样设置,不能设置相对于父容器,否则会产生

error: Error: String types not allowed (at 'width' with value 'wrap_content')。

并且,一个组件可以只有Layout _width ,layout_height。但却不能只有heigh ,width,而没有Layout _width ,layout_height,因为那样的组件会看不到。如果你要使用heigh ,width的话,就要先设置Layout _width ,layout_height,把heigh ,width用来作为组件的微调使用。

Xml:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical" >  
  5.   
  6.     <Button  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="hello talk"  
  10.         android:textColor="#482" />  
  11.   
  12.     <Button  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:height="80dp"  
  16.         android:text="hello talk"  
  17.         android:textColor="#810"  
  18.         android:width="80dp" />  
  19.   
  20.     <TextView  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="hello talk"  
  24.         android:textColor="#482" />  
  25.   
  26.     <TextView  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:height="80dp"  
  30.         android:text="hello talk"  
  31.         android:textColor="#810"  
  32.         android:width="80dp" />  
  33.   
  34.     </LinearLayout>  


截图:

可以看出来不是改变的字体,而是直接改变的组件本身,但在TextvView中,好像效果不是很明显。

我们知道,其实在设置宽高的时候,我们是可以直接的把宽高设置为定制为:xdp,在这方面,Layout _width ,layout_height或者heigh ,width一样都可以设置。

至于还有其他的区别,有待发现中,呵呵!!

去网上找了下相关的,有这种说法:

若还要讲讲两者的区别的话,那就是:
android:width 的值,一般是 "100dp" 这样的数值;
android:layout_width 的值,一般是"fill_parent","wrap_content","match_parent".当然,它也可以像前者一样,设置数值的.

带"layout"的属性是指整个控件而言的,是与父控件之间的关系,如 layout_gravity 在父控件中的对齐方式, layout_margin 是级别相同的控件之间的间隙等等;

不带"layout" 的属性是指控件中文本的格式,如gravity是指文本的对齐方式等等,而其中文本的格式又受制约于它的控件在父控件中的属性. 

至于说layout的属性是针对文本的,在这个例子中没有得到很好证实,因为对文本设置了宽高为 80dp,可是也没啥效果啊。

原文http://blog.csdn.net/yueqinglkong/article/details/10243551

抱歉!评论已关闭.