Android入门(6)——浅谈Android开发五大布局

来源:互联网 发布:机器人走迷宫编程 编辑:程序博客网 时间:2024/05/25 18:09

线性布局LinearLayout

1. 它包含的子控件将以横向或者竖向来排列。

2. 属性:


orientation决定的是线性布局内部水平或竖直,而gravity决定的是整个线性布局在手机页面中的摆放位置。


3. 使用线性布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" android:orientation="horizontal"android:gravity="center_horizontal"</LinearLayout>

4. 子类控件在LinearLayout中常用到的属性:

举例layout_gravity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" android:orientation="horizontal"android:gravity="center_horizontal"      // 这里需要注意<TextView    android:layout_gravity="center_vertical"      // 这里需要注意    android:id="@+id/textView2"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="TextView" /><TextView    android:id="@+id/textView1"      // 这里没有    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="TextView" /><TextView    android:id="@+id/textView3"      // 这里也没有    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="TextView" /></LinearLayout>

解释一下: gravity是在LinearLayout中设置的,设置的是整个线性布局,而layout_gravity是在内部的控件中设置的,是对内部的某个单个进行设置的。


举例layout_weight:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" android:orientation="vertical"android:gravity="center_vertical"><TextView    android:layout_weight="2"            // 这里注意    android:id="@+id/textView2"    android:layout_width="match_parent"    android:layout_height="wrap_content"    // 这里注意,必须使用这个,不然会出现问题,或者说比例会颠倒    android:text="TextView" /><TextView    android:layout_weight="1"           // 这里注意    android:id="@+id/textView1"    android:layout_width="match_parent"    android:layout_height="wrap_content"      // 这里注意    android:text="TextView" /><TextView    android:layout_weight="1"         // 这里注意    android:id="@+id/textView3"    android:layout_width="match_parent"    android:layout_height="wrap_content"      // 这里注意    android:text="TextView" /></LinearLayout>

关于layout_weight的数值,就是根据你设置的数据为比例来配置的。

5. 布局之内还可以套用布局,可以再拖入布局!!!



相对布局RelativeLayout

1. 特点:

它包含的子控件将以控件之间的相对位置或者子类控件相对父类容器的位置的方式排列。

相对布局可以任意摆放。

2. 属性:

下面是相对父容器的一个位置的属性(有对应的right和bottom):


举例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:paddingBottom="@dimen/activity_vertical_margin"      android:paddingLeft="@dimen/activity_horizontal_margin"      android:paddingRight="@dimen/activity_horizontal_margin"      android:paddingTop="@dimen/activity_vertical_margin"      tools:context=".MainActivity" >        <Button          android:id="@+id/button1"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignParentTop="true"          android:layout_alignParentBottom="true"          android:layout_alignParentRight="true"          android:text="一" />    </RelativeLayout>  

举例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      tools:context=".MainActivity" >        <Button          android:id="@+id/button1"          android:layout_width="match_parent"     // 这里注意          android:layout_height="match_parent"    // 这里注意          android:layout_marginLeft="25dp"          android:layout_marginTop="30dp"          android:text="一" />    </RelativeLayout> 

3. 下面是子类控件相对子类控件的位置设置:


举例layout_alignBaseline文字对其而已:



仅仅是把文字对齐啊,与layout_alignTop之类的这些不一样。

但是,但是,layout_alignTop之类的这些为了对其,是会裁剪的!!!!!或者会移位的!!!!具体效果还是自己以后试吧,我很懒。。。这里就不给效果图了。。。。


4. 举例

<?xml version="1.0" encoding="utf-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical" >        <TextView          android:id="@+id/textView1"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignParentLeft="true"          android:layout_alignParentTop="true"          android:layout_marginLeft="77dp"          android:layout_marginTop="63dp"          android:text="狗不理包子" />        <ImageView          android:id="@+id/imageView1"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignParentLeft="true"          android:layout_alignTop="@+id/textView1"          android:layout_marginLeft="22dp"          android:src="@drawable/ic_launcher" />        <TextView          android:id="@+id/textView2"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignLeft="@+id/textView1"          android:layout_below="@+id/textView1"          android:text="20元一个" />        <TextView          android:id="@+id/textView3"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignLeft="@+id/textView2"          android:layout_below="@+id/textView2"          android:text="又贵又不好吃" />    </RelativeLayout>  


帧布局FrameLayout

1. 特点

在这个布局中,所有的子元素都不能被制定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。

就是说直接拖拉,都会覆盖的。换句话说,控件之间是可以重叠的!!!


2. 举例:

<?xml version="1.0" encoding="utf-8"?>  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"             >      <!-- 没有gravity属性 -->        <TextView          android:layout_gravity="center"          android:background="#785435"          android:id="@+id/textView1"          android:layout_width="200dp"          android:layout_height="200dp"          android:text="第一个页面" />        <TextView          android:layout_gravity="center"          android:background="#985764"          android:id="@+id/textView2"          android:layout_width="150dp"          android:layout_height="150dp"          android:text="第二个页面" />        <TextView          android:layout_gravity="center"          android:background="#564238"          android:id="@+id/textView3"          android:layout_width="100dp"          android:layout_height="100dp"          android:text="第三个页面" />        <TextView          android:layout_gravity="center"          android:background="#697524"          android:id="@+id/textView4"          android:layout_width="80dp"          android:layout_height="80dp"          android:text="第四个页面" />    </FrameLayout>

效果图:

用这个例子很容易实现霓虹灯的效果。


3. 举例:

用ProgressBar控件(进度圈)和一个TextView控件显示进度:

<?xml version="1.0" encoding="utf-8"?>  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent" >        <ProgressBar          android:layout_gravity="center"          android:id="@+id/progressBar1"          style="?android:attr/progressBarStyleLarge"// 这里有点意思          android:layout_width="wrap_content"          android:layout_height="wrap_content" />        <TextView          android:layout_gravity="center"          android:id="@+id/textView1"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="80%" />    </FrameLayout> 
效果图:

当然这里的80%还并不会变化,不过在以后的学习中,我们可以改变TextView的内容来实现动态显示进度条。





绝对布局AbsoluteLayout

1. 又可以称作为坐标布局,可以直接指定子元素的绝对位置(xy),可以随意摆放,但都配备有xy值。

在实际使用中很少使用,因为手机屏幕大小不同,所以很麻烦。

真的很少用的,所以下面的不用看好了!

2. 属性:






表格布局TableLayout

1. 特点:

TableLayout表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象。

2. TableLayout的属性:


第二个shrinkColumns表示设定某行的宽不可以超出,但可以增加行显示,比如文字太多的时候会换行显示,但好像只对最后一个有效,没搞懂

第三个stretchColumns表示设定某行可以铺满剩余的宽度。这个没有必要是对最后一个有效。那如果想让每一列都沾满或者说均等大小,那么就把每一列都写上。


3. TableLayout中的子控件的属性:


设置子控件在TableLayout的占位。



























0 0
原创粉丝点击