Android (二)布局文件学习与手机登录界面实现

Android常用的五种布局
  • 线性布局(LinearLayout)
  • 帧布局(FrameLayout)
  • 表格布局(TableLayout)
  • 相对布局(RelativeLayout)
  • 绝对布局(AbsoluteLayout)
线性布局(LinearLayout)

LinearLayout:现行布局,可分为垂直布局(vertical)和水平布局(horizontal)子控件按垂直或水平排布。
特点一行一个或一列一个
垂直或水平属性设置使用android:orientation
简单垂直布局code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="昨天520,今天521 单身狗无处去"
        />
    
    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Goodby World!"
        />

</LinearLayout>

其中"match_parent"大小跟随父控件,"wrap_content"为根据内容确定大小。
界面:
在这里插入图片描述
FrameLayout: 帧布局不能为 一个子元素指定一个位置,而是后面的控件会挡住前一个控件,层层叠加。
demo

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

	<TextView
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:text="帧布局" />
	<Button 
	    android:layout_width="wrap_content"
    	android:layout_height="wrap_content" 
    	android:text="按钮"
    	android:layout_gravity="center"
	    />
</FrameLayout>

图形
在这里插入图片描述
TableLayout:表格布局,控件们分行或列排布,一个表格布局由许多TableRow组成,每个控件在其中就是一个单元格。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >
   
   <TableRow 
       android:layout_width="wrap_content"
   	android:layout_height="wrap_content"
   	
   	<TextView
   	    android:layout_width="wrap_content"
   		android:layout_height="wrap_content" 
   		android:text="tv1"
   		android:textSize="19sp"
       />
   <TextView
   	    android:layout_width="wrap_content"
   		android:layout_height="wrap_content" 
   		android:text="tv2"
       />
   	</TableRow>
       <TableRow 
       android:layout_width="wrap_content"
   	android:layout_height="wrap_content"
   	
   	<TextView
   	    android:layout_width="wrap_content"
   		android:layout_height="wrap_content" 
   		android:text="tv3"
       />
   <TextView
   	    android:layout_width="wrap_content"
   		android:layout_height="wrap_content" 
   		android:text="tv4"
   		android:textSize="19sp"
       />
   	</TableRow>
</TableLayout>

界面
在这里插入图片描述
RelativeLayout:相对布局,控件位置根据相对位置来确定,是最常用最灵活的布局,可左可右,可上可下进行对齐。
常用属性

        android:layout_below="@+id/tv"   //在tv控件下
        android:layout_above=""          //在某控件上
        android:layout_toLeftOf=""		 //在某控件左
        android:layout_toRightOf="" 	 //在某控件右
        android:layout_alignLeft=""		 //与某控件左对齐 	
        android:layout_alignRight=""	 //与某控件右对齐

还有一些:
在这里插入图片描述
deno

<?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/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="标签"        
        />
    <EditText 
        android:id="@+id/"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="文本框"  
        android:layout_below="@+id/tv" 	 
        />

</RelativeLayout>

效果图
在这里插入图片描述
AbsoluteLayout: 绝对布局可以让控件在指定的 x/y 坐标值,并显示在屏幕上。(0, 0)为左上角,并允许重叠。
demo

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    

	<EditText
 		android:text="Welcome to Mr Wei's blog"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
		/>
	<Button
 		android:layout_x="250px" 
 		android:layout_y="40px"
 		android:layout_width="70px" 
 		android:layout_height="wrap_content"
 		android:text="Button"
	    />


</AbsoluteLayout>

效果
在这里插入图片描述
不是非用不可的情况下,不推荐使用绝对布局,代码过于固定,缺少普遍性。

登录界面设计

布局

<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"
    tools:context=".MainActivity" >
    
    <EditText 
        android:id="@+id/user_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户名"
        />
     <EditText 
        android:id="@+id/user_passwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:password="true"
        android:hint="密码"
        />
     <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         >
         <CheckBox 
             android:id="@+id/check_box"
        	 android:layout_width="wrap_content"
       	 	 android:layout_height="wrap_content"
        	 android:hint="记住密码"
             />
         <Button 
             android:id="@+id/btn_login"
             android:onClick="click_login"
        	 android:layout_width="wrap_content"
       	 	 android:layout_height="wrap_content"
       	 	 android:layout_alignParentRight="true"
        	 android:hint="登录"
             />
         
         
     </RelativeLayout>


</LinearLayout>

MainActivity.java

package com.example.login521;

import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private EditText user_name;
	private EditText user_passwd;
	private CheckBox cb_isCheck;


	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        user_name = (EditText)findViewById(R.id.user_name);
        user_passwd = (EditText)findViewById(R.id.user_passwd);
        cb_isCheck = (CheckBox)findViewById(R.id.check_box);
        
        //上一次登录信息显示
        Map<String, String> maps = UserInfoUtils.readInfo();
        if(maps != null){
        	String username = maps.get("user");
        	String passwd = maps.get("passwd");
        	
        	user_name.setText(username);
        	user_passwd.setText(passwd);
        	
        }
        
        
    }
	//登录按钮的单击事件
	public void  click_login(View v) {
		String username = user_name.getText().toString().trim();
		String passwd = user_passwd.getText().toString().trim();
		//
		if(TextUtils.isEmpty(username) || TextUtils.isEmpty(passwd)){
			
			Toast.makeText(MainActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
			return;	
		}
		else{
			
			System.out.println("user"+username+":"+"passwd"+passwd);
		}
		
		if(cb_isCheck.isChecked()){
			boolean ret = UserInfoUtils.saveInfo(username, passwd);
			if(ret){
				
				Toast.makeText(MainActivity.this, "用户保存成功", Toast.LENGTH_SHORT).show();
				return;
				
			}
			else{
				Toast.makeText(MainActivity.this, "请选中保存密码", Toast.LENGTH_SHORT).show();
				return;
			}
			
		}
		
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

自定义油条类

package com.example.login521;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class UserInfoUtils {
	public static boolean saveInfo(String user,String passwd) {
		
		try {
			String result = user + "#" + passwd;
			File file = new File("/data/data/com.example.login521/login.txt");
			FileOutputStream fout = new FileOutputStream(file);
			fout.write(result.getBytes());
			fout.close();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}
	
	
	public static Map<String,String> readInfo() {
		
		try {
			Map<String, String> maps = new HashMap<String, String>();
			File file = new File("/data/data/com.example.login521/login.txt");
			FileInputStream inStream = new FileInputStream(file);
			//创建存放数据的buffer
			BufferedReader buffer = new BufferedReader(new InputStreamReader(
					inStream));
			String content = buffer.readLine();
			String[] splitsUser =  content.split("#");
			String username = splitsUser[0];
			String passwd = splitsUser[1];
			maps.put("user", username);
			maps.put("passwd", passwd);
			
			inStream.close();
			return maps;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}

	}
}

效果图
在这里插入图片描述

总结

Android好几个布局,但其实常用的也就线性布局、相对布局、表格布局。
写Android程序需要一点点java基础,其他好像和qt等差不太多。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值