2015 July 10


###LinearLayout v.s Relativelayout

  • When you compile your application, each XML layout file is compiled into a View resource.

  • There are a number of other ID resources that are offered by the Android framework. When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace, like so:android:id="@android:id/empty";

With the android package namespace in place, we’re now referencing an ID from the android.R resources class, rather than the local resources class.

Defining IDs for view objects is important when creating a RelativeLayout. In a relative layout, sibling views can define their layout relative to another sibling view, which is referenced by the unique ID.

  • Layout Parameters:Every ViewGroup class implements a nested class that extends ViewGroup.LayoutParams. This subclass contains property types that define the size and position for each child view, as appropriate for the view group. As you can see in figure 1, the parent view group defines layout parameters for each child view (including the child view group).

In general, specifying a layout width and height using absolute units such as pixels is not recommended. Instead, using relative measurements such as density-independent pixel units (dp), wrap_content, or match_parent, is a better approach, because it helps ensure that your application will display properly across a variety of device screen sizes.

  • Layout Position:For instance, when getLeft() returns 20, that means the view is located 20 pixels to the right of the left edge of its direct parent.

  • Size, Padding and Margins:The size of a view is expressed with a width and a height. A view actually possess two pairs of width and height values.

Padding can be used to offset the content of the view by a specific number of pixels.(可以设定合适的像素值去撑大View内容)

width and height定义组件在其Parent的实际大小

drawing width and drawing height 定义组件其实际显示大小



  • layout_weight:
    按准确比例显示LinearLayout内各个子控件: 如果想个权重值对于子控件正常的发挥作用,水平方向需设置android:layout_width=”0dp”;竖直方向需设置android:layout_height=”0dp”;
    在这种情况下某子个控件占用LinearLayout的比例为:本控件weight值 / LinearLayout内所有控件的weight值的和。




To improve the layout efficiency when you specify the weight, you should change the width of the EditText to be zero (0dp). Setting the width to zero improves layout performance because using “wrap_content” as the width requires the system to calculate a width that is ultimately irrelevant because the weight value requires another width calculation to fill the remaining space.

  • Selector 状态问题: Selector-Google

  • A RelativeLayout is a very powerful utility for designing a user interface because it can eliminate nested view groups and keep your layout hierarchy flat, which improves performance. If you find yourself using several nested LinearLayout groups, you may be able to replace them with a single RelativeLayout.

  • 视图解析机制:pull/sax/dom:pull机制比Sax机制更加灵活,控制的灵活性更强。


FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that’s scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

Child views are drawn in a stack, with the most recently added child on top. The size of the FrameLayout is the size of its largest child (plus padding), visible or not (if the FrameLayout’s parent permits).



  • android:foreground:设置该帧布局容器的前景图像
  • android:foregroundGravity:设置前景图像显示的位置
Loading Disqus comments...
Table of Contents