android的布局!

来源:互联网 发布:mac 双系统 删除 编辑:程序博客网 时间:2024/05/21 22:58
 

今天学的很蛋疼,学的不错(自我感觉),最起码老师上课讲的我都已经实现!作业只用表格布局和线性布局完成了,帧布局虽然不是你说的霓虹灯,但是成功的实现了动画!

老师讲的布局,首先布局分为四种(老师今天讲的四种,)分别是:

      线性布局

      表格布局

      相对布局

       帧布局

 

 

在String.xml下建立

<string name="app_name">布局演示</string>

    <string name="name_text">请输入用户名</string>

    <string name="ok_text">确定</string>

    <string name="cancel_text">取消</string>

   

    <string name="name">姓名</string>   

    <string name="gender">性别</string>   

    <string name="age">年龄</string>   

    <string name="phone">电话</string>

       

    <string name="nameZs">张三</string>   

    <string name="genderZs">男</string>   

    <string name="ageZs">28</string>   

    <string name="phoneZs">1112</string>

       

    <string name="nameLs">李四</string>   

    <string name="genderLs">女</string>   

    <string name="ageLs">21</string>   

    <string name="phoneLs">121221</string>

 

然后在layout下建立linear_layout.xml(线性布局)开始写程序:代码如下:

<?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"

 

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/app_name" //这句话就是使用刚才创建的string下的app_name,,当程序编译成功的时候会显示上面的“布局演示”。

        android:id="@+id/name_info"//这句话就是将“TextView”包装成一个模块(自己的理解)这个模块的地址就是@+id/name_info。以便于将下边的模块放在和它关于的位置好方便(都是自己的理解)

        />

       

     <EditText   //这是建立一个能输入文字的文本框

        android:layout_width="fill_parent"  //文本框的宽度

        android:layout_height="wrap_content"  //文本矿的高度

        android:layout_below="@id/name_info"   //这就是用到了上边定义的模块,它的意思就是把文本框的位置放在刚才模块(@id/name_info)的below的位置(就是下边)

        android:id="@+id/username"/>  //把“EditText”包装成一的模块,它的地址是:“@+id/username”,以便于下面存放位置

       

    

     <Button    //建立一个模块(按钮)

        android:layout_width="wrap_content"  //按钮的宽度

        android:layout_height="wrap_content"//按钮的高度

        android:text= "@string/cancel_text"    //这句话就是将String下的”canael_text”使用,就是取消按钮

        android:layout_alignParentRight="true"   //这句话就是把“取消”按钮放在右边

        android:layout_below="@id/username"    //把这个“模块”放在"@id/username"(上面第一的文本框模块)的下面

        android:id="@+id/cancel_button" //然后本自己“Butten”包装成一个模块,

模块的入口(地址)是@+id/cancel_button。

 

       //这几天属性就是将button(取消)按钮放在了上边的文本框的右下边,并且定义了按钮的宽和高!这个模块实现的功能。

        />

      <Button   //建立另一个button按钮(确定按钮)

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/ok_text"  //在String下实现了“确定”按钮,是有ok_text导入到String里面

        android:layout_toLeftOf="@id/cancel_button"   //将这一个按钮放在上一个模块的左边,有“@id/cancel_button”导入到上个模块(确定按钮模块)

        android:layout_below="@id/username"//将这个确定按钮放在文本框的下面

        />

       

 

</RelativeLayout>

以上都是我不成熟的理解,还请老师指点!我就是这么理解的!

 

 

 

表格布局:

<?xml version="1.0" encoding="utf-8"?>

<TableLayout  //表格布局的权限是“Table“     xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

     android:stretchColumns="0,1,2,3">    //由于表格是四列,生成后显示的和屏幕的大小有差距,加上这句话,表格的大小会和屏幕相当!(就是比原来的大,比原来的好看)

 

   <TableRow >  //建立的第一行,由于是建立的表格布局,所以一行会建立4列!所以“< android:stretchColumns="0,1,2,3" >这句话实现了4列的大小比例

 

       <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="@string/name"/> //姓名

       <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="@string/gender" //性别

           />

        <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="@string/age"    //年龄

           />

         <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="@string/phone"  //电话号码

           />          

      

   </TableRow>

  

   <TableRow >        //这是建立的第二行

       <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="@string/nameZs"/>   //  张三的名字,和姓名一列

       <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="@string/genderZs"//张三的性别,和性别一列

           />

        <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="@string/ageZs"//张三的年龄。和年龄一列

           />

         <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="@string/phoneZs"//张三的电话。和电话一列

           />          

      

   </TableRow>

  

   <TableRow >//建立的第三行

       <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="@string/nameLs"/>

       <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="@string/genderLs"

           />

        <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="@string/ageLs"

           />

         <TextView

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="@string/phoneLs"

           />          

      

   </TableRow>

 

</TableLayout> “

 

 

       所以在value建立的String.xml先当于一个数据库,在布局中使用的情况下会自动调用(自己的理解)

 

 

 

相对布局

代码如下

<?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" >

 

    <TextView

        android:id="@+id/textView1"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/name_text" />

 

 

    <EditText

        android:id="@+id/editText1"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/textView1"/>

   

    <Button

        android:id="@+id/cancel_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentRight="true"

        android:layout_below="@id/editText1"

        android:text="@string/cancel_text" />

 

    <Button

        android:id="@+id/ok_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@id/editText1"

        android:layout_toLeftOf="@id/cancel_button"

        android:text="@string/ok_text" />

 

</RelativeLayout>

 

我感觉这个和线性布局差不多!大同小异!

帧布局:

package cn.class3g.activity;

 

import android.app.Activity;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.FrameLayout;

 

public class FrameLayoutTestActivity extends Activity {

  private boolean flag = true;

  FrameLayout frame = null;

 

  protected void onCreate(Bundle savedInstanceState) {

     // TODO Auto-generated method stub

     super.onCreate(savedInstanceState);

     this.setContentView(R.layout.main);

     findViews();

 

     final MyHandler myHandler = new MyHandler();

 

     myHandler.sleep(10);

 

     frame.setOnClickListener(new OnClickListener() {

 

       public void onClick(View v) {

         flag = !flag;

         myHandler.sleep(10);

 

       }

 

     });

 

  }

 

  private void findViews() {

     frame = (FrameLayout) this.findViewById(R.id.frame);

  }

 

  class MyHandler extends Handler {

     int i = 0;

 

     @Override

     public void handleMessage(Message msg) {

       i++;

       show(i % 4);

       sleep(100);

     }

 

     public void sleep(long delayMillis) {

       if (flag) {

         this.sendMessageDelayed(obtainMessage(10), delayMillis);

       }

     }

 

  }

 

  private void show(int j) {

     Drawable pic[] = new Drawable[4];

     pic[0] = this.getResources().getDrawable(R.drawable.p1);

     pic[1] = this.getResources().getDrawable(R.drawable.p2);

     pic[2] = this.getResources().getDrawable(R.drawable.p3);

     pic[3] = this.getResources().getDrawable(R.drawable.p4);

//   for (int i = 0; i < pic.length; i++) {

//     pic[i] = this.getResources().getDrawable(R.drawable.p1 + 1+i);

//   }

 

     frame.setForeground(pic[j]);

  }

 

}

 

我的bug问题

我的问题的bug其实很简单,为什么他一执行就会强行停止,?

听我解释慢慢道来:

上课跟着老师写代码,老师建立的那个Android project下src的名字太长,我就随便写了一个,等课下写完后,我就把把名字改成“FrameLayoutTestActivity”,但是由于在“AndroidMainfest.xml”下还是那个我开始定义的名字。相当于表面改名字,但是内部没有改,所以一执行模拟器会自动停止,有这个文件,但是模拟器进不去,就会自动的强行停止!

 

当帧布局运行时会出现不同的画面
原创粉丝点击