android笔记(3)--布局

现在学习android布局。

首先看图,理清关系。

android有absoutelayout(绝对)、frameLayout(帧)、linerLayout(线性)、tableLayout(表格)、relativeLayout(相对)、gridView(网格)六大布局,其中linearLayout可以看做tableLayout的特殊布局。GridView是android4.0后新增的布局。


so,所有布局都是ViewGroup的子类,ViewGroup是View的子类


一、LinearLayout 布局

linearLayout布局没啥特别,看个例子。

line.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"  //对齐方式,horizontal水平,vertical垂直
    android:divider="#00c"
    android:gravity="top|center"  //布局内容器对齐方式
    >
    
    <Button 
        android:id="@+id/bot1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/bot1"
        />
    
    <Button 
        android:id="@+id/bot2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/bot2"
        />
    

</LinearLayout>

MainActivity.java

package com.example.linelayout;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.line);
	}

	@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;
	}

	
}

string.xml

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

    <string name="app_name">lineLayout</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
	<string name="bot1">bot1</string>
    <string name="bot2">bot2</string>
</resources>

效果图如下



二、tableLayout布局

  • 不需要明确声明多少行多少列
  • 列的宽度由最宽的决定

下面来构建一个简单计算器界面


layout->table.xml

<?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"
    android:orientation="horizontal"
    android:id="@+id/root"
     >
    
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="50sp"
        android:background="#eee"
        android:textColor="#000"
        android:text="@string/init"
        android:layout_margin="5px"
        />
    
    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/clear"
        />
    
    
    

</TableLayout>

string.xml

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

    <string name="app_name">tableLayout</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
	<string name="init">0</string>
	<string name="clear">清除</string>
	
	
</resources>

MainActivity.java

package com.example.tablelayout;

import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.Menu;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;

public class MainActivity extends Activity {
	TableLayout tableLayout;
	String[] chars = new String[]{
		"7", "8", "9", "/",
		"4", "5", "6", "x",	
		"1", "2", "3", "-",	
		".", "0", "=", "+"
	}; 
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.table);
		
		//获取屏幕宽+高
		DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
		
		tableLayout = (TableLayout) findViewById(R.id.root);
		int len = chars.length/4;
		for(int i = 0; i < len; i++){
			TableRow tr = new TableRow(this);
			
			for(int j = 0; j < 4 ; j++){
				Button bot = new Button(this);
				bot.setWidth((int) ((width)*0.25));
				bot.setHeight((int) ((height)*0.15));
				bot.setText(chars[i*4+j]);
				tr.addView(bot);
			}
			
			tableLayout.addView(tr);
		}
		
	}

	@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;
	}

}

效果图为:



其他布局没有怎么研究。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值