Android之LinearLayout学习

来源:互联网 发布:淘宝店过户 编辑:程序博客网 时间:2024/05/17 04:46

LinearLayout(线性布局)

1.将控件排列为一行或一列,这取决于它的方向orientation 属性设置。线性布局分为水平线性和垂直线性二者的属性分别为: 

android:orientation= " horizontal "  水平线性布局   

android:orientation= "vertical"     竖直线性布局

2.android:gravity作用是控制布局文件中控件的对齐方式。当对没有子控件的控件设置时,此时指的是控件内容相对控件的对齐方式。例子如下:

   <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        

        android:orientation="horizontal" >

 

        <Button

            android:gravity="right|top" //设置为右上方,不会改变控件本身位置

            android:layout_width="150dp"

            android:layout_height="150dp"

            android:text="确定" 

            android:textSize="30sp"

            />

 

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="取消" />

</LinearLayout>

效果图


当对有子控件的父控件设置时,此时指的是子控件相对于父控件的对齐方式。

<LinearLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:gravity="right" //设置下面的子控件右对齐父控件

        android:orientation="horizontal" >

 

        <Button

            android:gravity="right|top"//设置为右上方,不会改变控件本身位置

            android:layout_width="100dp"

            android:layout_height="100dp"

            android:text="确定" 

            android:textSize="30sp"

            />

 

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="取消" />

</LinearLayout>

效果图


3.android:layout_weight作用是设定该控件所占整个界面布局的比重。

 <TextView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_weight="1"

        android:background="#ff1493" />

 

    <TextView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_weight="1"

        android:background="#b0e0e6" />

注释:总的weight2,两个各占一半。






 


0 0
原创粉丝点击