android开发之UI介绍

来源:互联网 发布:中岛健人 知乎 编辑:程序博客网 时间:2024/06/09 23:47
## UI开发 ##
UI的排布都是View和ViewGroup组成。    
View:绘制在屏幕上用于和用户进行交互的内容。   
ViewGroup:包含View和ViewGroup的对象。一般用来定义布局。比如LinearLayout和RelativeLayout。
布局的嵌套层级不能过多,超过10层就会将当前的应用性能大大降低。

ViewGroup是用来定义布局的。布局中可以排布控件。

在页面中如果需要显示一个控件或者布局,那么这个控件或者布局必须具有宽和高。
android:layout_width="match_parent"    宽度   
android:layout_height="match_parent"   高度   
取值:
match_parent:匹配父类,即父类有多大,就匹配多大。
wrap_content:包裹内容。内容有多大就呈现多大。


关于布局的讲解:   
常用的布局:
LinearLayout线性布局
RelativeLayout相对布局
FrameLayout:帧布局
AbsoluteLayout绝对布局
TableLayout:表格布局
GridLayout:网格布局
 8-2
**LinearLayout:线性布局**    
水平或者垂直进行排布。
默认是按照水平进行排布。界面排布的时候元素要可见。否则用户交互不到就没有任何意义。



margin:当前控件相对于其他的控件的距离。相当于距离上下左右。
marginLeft:距离左边有多少间距。

layout_gravity:表示控件相对于父类布局的排布方式。
注意特点:如果在线性布局整体是水平排布的时候,layout_gravity只在垂直方向上有作用。同样反之亦然。   
gravity:表示内部内容的排布方式。加在布局中表示布局内部控件的排布方式。加在控件上表示控件的内部内容相对于控件自身的排布方式。


**layout_weight:权重**:按照百分比的形式进行划分。----只存在于线性布局
划分的时候一般会根据比值进行计算,如果划分的方向是使用的是wrap_content,那么权重越大,所占的比值越大,如果是match_parent。那么权重越大,所占的比值越小。
可以尝试权重的比值大小。
weight_sum:可以指定当前划分的权重的总份数。

思考:有权重的控件和无权重的控件,系统先给谁分配空间? 无权重。


orientation:方向
horizontal:水平
vertical:垂直

**RelativeLayout:相对布局**
存在相对的概念。其中的控件可以任意进行拖拽。
一般在排布的时候都有参照物的概念,所以在找到参照物都是按照id。
所以在相对布局中一般都会给使用的控件定义ID。

  android:id="@+id/haha":表示系统当中原本不存在这个id,相当于将这个id添加到项目中。
 @id/haha:使用id,表示该id已经存在直接使用。

layout_toRightOf:在某个对应id的控件的右边     
layout_toLeftOf:在某个控件的左边    
android:layout_centerHorizontal="true"是否水平居中   
android:layout_centerVertical="true":是否垂直居中    
android:layout_centerInParent="true":父容器居中   
layout_below:在某个控件的下方   
layout_above:在某个控件的上方   
layout_alignParentTop:父容器顶部对齐   
layout_alignParentRight:父容器右部对齐   
layout_alignParentBottom:父容器的底部对其
layout_alignParentLeft:父容器的左部对其    
layout_alignRight:相对于某个控件右部对齐。其中方向性的单词可以更换    
layout_alignBaseline:与某个控件基准线对齐    

**帧布局:FrameLayout**   
按照画面的形式进行排布。显示出一帧一帧的概念。   
最先排布的控件位于帧布局的最下方画面。最后定义的控件位于最上方。   

举例:侧滑菜单。手机联系人的索引字母   

**绝对布局:AbsoluteLayout**   
 AbsoluteLayout:绝对布局已经不推荐使用。   
 因为出现的是早期的版本。   
 按照绝对的像素点位置进行排布。类似于按照坐标。   
 操作使用的是layout_x   
 layout_y
 定制手机定制软件有可能使用。

**表格布局TableLayout**
TableLayout继承自LinearLayout,所以可以使用权重的属性
排布的时候通过的是tableRow
在表格布局中如果单独出现一个控件也是当成一行进行处理的。
android:stretchColumns="1,2,3"   拉伸  参数为列数
android:shrinkColumns="2"       回缩  参数为列数
表格布局在排布的时候都是比较规范的


     <TableLayput
        android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:shrinkColumns="2"
         android:stretchColumns="1,2,3">
         <TableRow>
            <TextView/>
         </TableRow>
        <TableRow>
            <TextView/>
         </TableRow>    </TableLayout>
注意:
任何空间如果需要在二维平面中进行显示,就必须指定宽和高(width/height)
但对TableRow可以不用指定,并且其内部的控件或者充当tableRow的控件也可以不指定宽和高,他会遵循系统的宽和高


**网格布局GridLayout**  
出现在android 4.0之后的布局,最低版本必须是14

请对比TableLayout和GridLayout有什么不同?
 * tableLayout是表格布局,是按照tableRow的行的概念进行排布的,可以实现合并列。
 * 而网格布局GridLayout是可以随时指定行和列的个数,并且既可以实现合并行也可以实现合并列。
 * 只不过是4.0之后出现的。表格布局中如果需要拉伸或者回缩某一列都需要在布局本身上指定android:stretchColumns
 * 和android:shrinkColumns,但是网格布局GridLayout中只需要在某一个特定需要拉伸或者回缩的控件上给定rowSpan和
 * columeSpan就可以。
比如计算器按钮
     
       <GridLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:columnCount="4"    //4列>
   
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2" />  
     .....
     <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_rowSpan="2"     //行合并
        android:layout_gravity="fill"   //填满
        android:text="+" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnSpan="3"    //列合并
        android:layout_gravity="fill"
        android:text="=" />

     </GridLayout>
0 0
原创粉丝点击