Android开发Android知识首页投稿(暂停使用,暂停投稿)

# Android布局

2016-08-30  本文已影响178人  伍零一

Framelayout

AbsoluteLayout

能够使子View在某一个固定的位置.绝对布局缺乏灵活性,而且很难去维护相比于其他没有绝对位置的布局.

     <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="100dp"
            android:layout_y="100dp"
            android:text="文本"
            />
    </AbsoluteLayout>

Relativelayout

布局位置可以根据周围相互关联的布局或者父布局来设置.

官方提示:

Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.

不能用于一个循坏依赖的布局在相对布局尺寸和子布局之间.举个例子,当你Relativelayout的高度是WRAP_CONTENT,那么Relativelayout的尺寸是相对的,这个时候你设置子View 为ALIGN_PARENT_BOTTOM,也是依赖于父布局的底边的位置,这样是不行的.

在android系统版本在17级以下(包含17的时候)。使用measure会出现NULL异常情况,这个是一个BUG。原因是在RelativeLayout的控件使用在含有scrolling的时候,该含有scrolling的控件中计算空间大小的时候,没有使用MeasureSpec mode UNSPECIFIED的布局方式在RelativeLayout。自定义的控件则会尽可能的使用AT_MOST 来替换对齐方式。

如果你想解决这个问题有2个方法:

1.讲SDK的目标版本升级

2.将需要使用RelativeLayout的上层包一个LinearLayout即可.

属性 解释
Above 定义将元素的底边对齐另一个元素的顶边
BELOW 定义将元素的顶边对齐另一个元素的底边
属性 解释
align_baseline 定义将元素的基线对齐另一个元素的基线
align_bottom 定义将元素的底边对齐另一个元素的底边
align_left 定义将元素的左边对齐另一个元素的左边
align_right 定义将元素的右边对齐另一个元素的右边
align_top 定义将元素的顶边对齐另一个元素的顶边
属性 解释
align_parent_bottom 定义将元素的底边对齐父容器的底边
align_parent_left 定义将元素的左边对齐父容器的左边
align_parent_right 定义将元素的右边对齐父容器的右边
align_parent_top 定义将元素的顶边对齐父容器的顶边
属性 解释
CENTER_HORIZONTAL 定义元素在RelativeLayout中水平居中
CENTER_IN_PARENT 定义元素在RelativeLayout的中心
CENTER_VERTICAL 定义元素在RelativeLayout内垂直居中
属性 解释
LEFT_OF 甲元素的右边对齐乙元素的左边
RIGHT_OF 甲元素的左边对齐乙元素的右边

LinearLayout

A Layout that arranges its children in a single column or a single row. The direction of the row can be set by calling setOrientation(). You can also specify gravity, which specifies the alignment of all the child elements by calling setGravity() or specify that specific children grow to fill up any remaining space in the layout by setting the weight member of LinearLayout.LayoutParams. The default orientation is horizontal

一个可以将子元素分布到一列或者一行的布局.可以通过setOrientation来设置水平或者垂直方向.你也可以通过setGravity来确定重力方向.通过weight属性来设置元素在布局中占据的比例.

例子:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="文本1"
            android:layout_gravity="end"
            />
        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="文本2"
            android:layout_gravity="center_horizontal"
          />
    </LinearLayout>

2:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="文本1"
    android:layout_gravity="top"

    />
<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="文本2"
    android:layout_gravity="bottom"
  />
</LinearLayout>

android:gravity是相对于自己的位置,如button中的text相对自己的位置.

android:layout_gravity是相对于布局的位置.如屏幕居中.

android:layout_gravity只在LinearyLayout和FrameLayout中有效.

上一篇 下一篇

猜你喜欢

热点阅读