区别

来源:互联网 发布:淘宝网店需要授权吗 编辑:程序博客网 时间:2024/04/28 07:32

1,android中,ImageViewsrcbackground有什么区别呢

答:

src的时候,应是原图显示,不该变图片的大小;用background的时候,按照组件的大小来放大或者缩小图片

background当背景的。可以设置的参数是不同的。比如background是可以平铺的 

本帖最后由 煮咖啡 于 2011-10-9 09:56 编辑

ImageViewXML属性srcbackground的区别:以前在设置这个控件的时候,根本就没关心过这两个属性的区别,在我眼里,感觉两个都一样,呵呵,现在知道错了,这两个属性是不一样的,background会根据ImageView的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸。
ImageView的长宽都设置为:100px
下图设置的是background引用图片:
 
下图设置的是src引用图片
 


之前一直没有搞懂android:paddingandroid:layout_margin的区别,其实概念很简单,padding是站在父view的角度描述问题,它规定它里面的内容必须与这个父view边界的距离。margin则是站在自己的角度描述问题,规定自己和其他(上下左右)的view之间的距离,如果同一级只有一个view,那么它的效果基本上就和padding一样了。例如我的XML layout代码如下:

[xhtml] view plaincopy

<?xml version="1.0" encoding="utf-8"?>  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

    android:orientation="vertical"  

    android:layout_width="fill_parent"  

    android:layout_height="fill_parent"  

    android:paddingLeft="10dip"  

    android:paddingRight="10dip"  

    android:paddingTop="10dip"  

    android:paddingBottom="10dip"  

10     >  

11 <TextView    

12     android:layout_width="wrap_content"   

13     android:layout_height="wrap_content"   

14     android:background="#FF0000"  

15     android:text="@string/hello"  

16     android:paddingLeft="50dip"  

17     android:paddingRight="50dip"  

18     android:paddingTop="50dip"  

19     android:paddingBottom="50dip"  

20     android:layout_marginBottom="10dip"  

21     />  

22     <TextView    

23     android:layout_width="wrap_content"   

24     android:layout_height="wrap_content"   

25     android:background="#FF0000"  

26     android:text="@string/hello"  

27     android:paddingLeft="50dip"  

28     android:paddingRight="50dip"  

29     android:paddingTop="50dip"  

30     android:paddingBottom="50dip"  

31     android:layout_marginBottom="10dip"  

32     />  

33     <TextView    

34     android:layout_width="wrap_content"   

35     android:layout_height="wrap_content"   

36     android:background="#FF0000"  

37     android:text="@string/hello"  

38     android:paddingLeft="50dip"  

39     android:paddingRight="50dip"  

40     android:paddingTop="50dip"  

41     android:paddingBottom="50dip"  

42     android:layout_marginBottom="10dip"  

43     />  

44     <TextView    

45     android:layout_width="wrap_content"   

46     android:layout_height="wrap_content"   

47     android:background="#FF0000"  

48     android:text="@string/hello"  

49     android:paddingLeft="50dip"  

50     android:paddingRight="50dip"  

51     android:paddingTop="50dip"  

52     android:paddingBottom="50dip"  

53     android:layout_marginBottom="10dip"  

54     />  

55 </LinearLayout>  

 那么我会得到如下的效果,图上已经很明确的标出来区别咯。

 

3.Android中各种相似的区别

2011-05-09 16:46

1.View setVisibility有三个值可以设置:
    VISIBLE--->可见
    INVISIBLE--->不可见,但这个ViewViewGroupt中仍保留它的位置,不重新layout
    GONE---->不可见,但这个ViewViewGroupt中不保留位置,重新layout,那后面的view就会取代他的位置。

    注意:若你在Adapter中的getViewbindview将某些特殊的位置的View.INVISIBLE之后,要记得在别的判断条件下将其 View.Visible.不然你会看到很多怪现象的。

2.android:gravityandroid:layout_gravity

    LinearLayout有两个非常相似的属性:android:gravityandroid:layout_gravity。他们的区别在于:android:gravity用于设置View组件的对齐方式,而android:layout_gravity用于设置Container组件的对齐方式。
    举个例子,我们可以通过设置android:gravity="center"来让EditText中的文字在EditText组件中居中显示;同时我们设置EditTextandroid:layout_gravity="right"来让EditText组件在LinearLayout中居中显示。

3.android:paddingandroid:layout_margin

android:paddingandroid:layout_margin的区别,其实概念很简单,padding是站在父view的角度描述问题,它规定它里面的内容必须与这个父view边界的距离。margin则是站在自己的角度描述问题,规定自己和其他(上下左右)的view之间的距离,如果同一级只有一个view,那么它的效果基本上就和padding一样了。
AndroidMarginPaddingHtml的是一样的。通俗的理解 Padding 为内边框,Margin 为外边框

对应的属性为
android:layout_marginBottom="25dip"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:layout_marginRight="10dip"
android:paddingLeft="1dip"
android:paddingTop="1dip"
android:paddingRight="1dip"
android:paddingBottom="1dip"

如果左右上下都是相同的设置则可以直接设置

android:layout_margin="10dip"
android:padding="5dip" 
4.MATCH_PARENTFILL_PARENT
      过去只有FILL_PARENTWRAP_CONTENT 那么match_parent到底是什么类型呢其实从Android 2.2开始FILL_PARENT改名为MATCH_PARENT ,从API Level8开始我们可以直接用MATCH_PARENT来代替FILL_PARENT,他们的定义本质是一样均为 -1,只是换了个别名,可能为了更准确些,比如最终在SDK中的定义为:

fill_parent   -1  The view should be as big as its parent (minus padding). This constant is deprecated starting from API Level 8 and is replaced by match_parent. 
match_parent   -1 The view should be as big as its parent (minus padding). Introduced in API Level 8. 
wrap_content   -2  The view should be only big enough to enclose its content (plus padding).  

显示单位pxdip以及sp的区别

dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGAHVGAQVGA 推荐使用这个,不依赖像素。 

px: pixels(像素). 不同设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较多。 

pt: point,是一个标准的长度单位,1pt1/72英寸,用于印刷业,非常简单易用; 

sp: scaled pixels(放大像素). 主要用于字体显示best for textsize。 

由此,根据 google 的建议,TextView 的字号最好使用 sp 做单位,而且查看 

TextView 

的源码可知 Android 默认使用 sp 作为字号单位。 

################################################################### 

关于换算(以 sp 和 pt 为例) 

查看 TextView 等类的源码,可知: 

case COMPLEX_UNIT_PX: 

      return value; 

case COMPLEX_UNIT_SP: 

      return value * metrics.scaledDensity; 

case COMPLEX_UNIT_PT: 

      return value * metrics.xdpi * (1.0f/72); 

-------------------------- 

scaledDensity = DENSITY_DEVICE / (float) DENSITY_DEFAULT; 

xdpi = DENSITY_DEVICE; 

-------------------------- 

DENSITY_DEFAULT = DENSITY_MEDIUM = 160; 

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

所以: 假设 pt 和 sp 取相同的值 1,则可设 1pt 和 1sp 之间系数为 x, 

1 * DENSITY_DEVICE / 72 = x * 1 * DENSITY_DEVICE / 160  => 

x = 160 / 72 = 2.2222 

也就是说在 Android 中,  1pt 大概等于 2.22sp 

以上供参考,如果 UI 能够以 sp 为单位提供设计是最好的,如果设计中没有 sp 

的概念,则开发人员也可以通过适当的换算取近似值。 

什么是Dip和 Sp 

过去,程序员通常以像素为单位设计计算机用户界面。例如,定义一个宽度为300像素的表单字段,列之间的间距为5个像素,图标大小为16×16像素 等。这样处理的问题在于,如果在一个每英寸点数(dpi)更高的新显示器上运行该程序,则用户界面会显得很小。在有些情况下,用户界面可能会小到难以看清 内容。 

与分辨率无关的度量单位可以解决这一问题。Android支持下列所有单位。 

px(像素):屏幕上的点。 

in(英寸):长度单位。 

mm(毫米):长度单位。 

pt(磅):1/72英寸。 

dp(与密度无关的像素):一种基于屏幕密度的抽象单位。在每英寸160点的显示器上,1dp = 1px。 

dip:与dp相同,多用于 android/ophone示例中。 

sp(与刻度无关的像素):与dp类似,但是可以根据用户的字体大小首选项进行缩放。