android布局-android学习之旅(十五)

TableLayout 表格布局

tablelayout简介

表格布局有TableLayout代表,但是它的本质定义仍然是线性管理器。表格布局采用行和列来管理UI,但是不需要明确的定义多少行,多少列,而是通过添加TableRow,没添加一个TableRow,就添加一行,TableRow中每添加一个组件就是一列。

TableLayout属性

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">
   <TableLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:shrinkColumns="1"
        android:stretchColumns="2">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hello "/>
       <TableRow>
           <Button
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="hello "/>
           <Button
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="hello "/>
           <Button
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="hello "/>

       </TableRow>

       </TableLayout>
    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:collapseColumns="1">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hello "/>
        <TableRow>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hello "/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hello "/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hello "/>

        </TableRow>

    </TableLayout>
    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1,2">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hello "/>
        <TableRow>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hello "/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hello "/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hello "/>

        </TableRow>
        <TableRow>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hello "/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hello "/>
        </TableRow>

    </TableLayout>
</LinearLayout>

FrameLayout帧布局

帧布局是由FrameLayout所代表,FrameLayout直接继承了ViewGroup属性。帧布局为每一个加入的控件创建一个空白的区域,称为一帧,每个子组件占据一帧,这些帧都会根据gravity属性,自动对齐。

FrameLayout的属性

子组件会受到FrameLayout.layoutParam的控制,可指定

android:layout_gravity来控制对齐方式。

Frame霓虹灯实例

package peng.liu.testview;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends Activity {
    private int[] names = new int[]{
            R.id.view01,
            R.id.view02,
            R.id.view03,
            R.id.view04,
            R.id.view05,
            R.id.view06
    };
    private int[] colors = new int[]{
            R.color.color1,
            R.color.color2,
            R.color.color3,
            R.color.color4,
            R.color.color5,
            R.color.color6
    };
    private int currentColor = 0;
    TextView[] texts = new TextView[names.length];
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x123){
                for(int i =0;i<names.length;i++){
                    texts[i].setBackgroundResource(colors[(i + currentColor) % names.length]);
                }
                currentColor++;
            }
            super.handleMessage(msg);
        }
    } ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for(int i=0;i<names.length;i++){
            texts[i] = (TextView) findViewById(names[i]);
        }
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0x123)
            }
        },0,200);
    }
}

布局代码

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">
   <FrameLayout
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">
        <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/view01"
       android:layout_gravity="center"
       android:background="@color/color1"
       android:width="320px"
       android:height="320px"
       />
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/view02"
           android:layout_gravity="center"
           android:background="@color/color2"
           android:width="280px"
           android:height="280px"
           />
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/view03"
           android:layout_gravity="center"
           android:background="@color/color3"
           android:width="240px"
           android:height="240px"
           />
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/view04"
           android:layout_gravity="center"
           android:background="@color/color4"
           android:width="200px"
           android:height="200px"
           />
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/view05"
           android:layout_gravity="center"
           android:background="@color/color5"
           android:width="160px"
           android:height="160px"
           />
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/view06"
           android:layout_gravity="center"
           android:background="@color/color6"
           android:width="120px"
           android:height="120px"
           />
       </FrameLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="color1">#f00</color>
    <color name="color2">#0f0</color>
    <color name="color3">#00f</color>
    <color name="color4">#ff0</color>
    <color name="color5">#f0f</color>
    <color name="color6">#0ff</color>
</resources>
时间: 2025-02-01 17:00:35

android布局-android学习之旅(十五)的相关文章

Android学习笔记二十五之ListView多布局实现

Android学习笔记二十五之ListView多布局实现 这一节是介绍ListView这个控件的最后一节,实现一个Item的多布局.像我们经常在用的各种即时通讯工具,QQ.微信等,假设他们的会话界面是ListView实现的,那么ListView就有多种Item布局,这一节,我们就来实现一个ListView的多种Item. 要实现ListView里面有多种Item,就要重写适配器的两个方法getViewTypeCount()和getItemViewType(int position),第一个方法是

Android学习笔记(十五)——碎片的生命周期(附源码)

碎片的生命周期 点击下载源码 与活动类似,碎片具有自己的生命周期.理解了碎片的生命周期后,我们可以在碎片被销毁时正确地保存其实例,在碎片被重建时将其还原到前一个状态. 1.使用上一篇的项目Fragments,在Fragment1.java文件中添加如下代码: package net.zenail.Fragments; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragm

Android学习路线(十五)Activity生命周期——重新创建(Recreating)一个Activity

先占个位置,下次翻译~ :p There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish(). The system may also destroy your

Android之旅十五 android中的网络操作

android中的网络操作和java里面没有什么区别,java里面的很多网络操作方法都可以搬到android中去使用,主要几个点: 1.post和get请求的区别,大家可以在网上查阅有关资料进行了解,get主要以向地址中拼接字符串参数发送到服务器,长度有限制,并且请求参数暴露在地址栏中,不怎么安全:post则主要是将请求参数转换为相应的http协议请求体发送到服务器,相比get方式,参数的长度没有限制,并且参数信息不会暴露给用户: 2.我们在java web里面通过浏览器以post方式发送数据,

Android(Xamarin)之旅(五)

原文:Android(Xamarin)之旅(五) 2016年1月23日,北京迎来了很痛苦的一天,冻死宝宝了,一天都没有出我自己的小黑屋,在这屋子里自娱自乐.不知道你们呢 对于android的四大基本组件(Activity.Service.Broadcast Receiver.Content Provider), 前面已经介绍了Activity,最基本的活动单元,任何一个活动的处理,都需要他,前面介绍的对话框.布局.提示消息.基本内容设置等等.Broadcase Receiver,广播,主要是用于

基于 Android NDK 的学习之旅----- C调用Java

http://www.cnblogs.com/luxiaofeng54/archive/2011/08/17/2142000.html 基于 Android NDK 的学习之旅----- C调用Java许多成熟的C引擎要移植到Android 平台上使用 , 一般都会 提供 一些接口, 让Android sdk 和 jdk 实现. 下文将会介绍 C 如何 通过 JNI 层调用 Java 的静态和非静态方法. 1.主要流程 1.  新建一个测试类TestProvider.java a)       

【转】基于 Android NDK 的学习之旅-----数据传输(引用数据类型)

原文网址:http://www.cnblogs.com/luxiaofeng54/archive/2011/08/20/2147086.html 基于 Android NDK 的学习之旅-----数据传输二(引用数据类型)(附源码) 基于 Android NDK 的学习之旅-----数据传输(引用数据类型) 接着上篇文章继续讲.主要关于引用类型的数据传输,本文将介绍字符串传输和自定义对象的传输. 1.主要流程 1.  String 字符串传输 a)         上层定义一个native的方法

Android热修复学习之旅——HotFix完全解析

在上一篇博客Android热修复学习之旅开篇--热修复概述中,简单介绍了各个热修复框架的原理,本篇博客我将详细分析QQ空间热修复方案. Android dex分包原理介绍 QQ空间热修复方案基于Android dex分包基础之上,简单概述android dex分包的原理就是:就是把多个dex文件塞入到app的classloader之中,但是android dex拆包方案中的类是没有重复的,如果classes.dex和classes1.dex中有重复的类,当classes.dex和classes1

基于 Android NDK 的学习之旅-----序言

http://www.cnblogs.com/luxiaofeng54/tag/android/default.html?page=1 基于 Android NDK 的学习之旅-----目录 duicky 2011-09-15 16:34 阅读:3035 评论:2 基于 Android NDK 的学习之旅-----资源释放 duicky 2011-08-21 14:29 阅读:6173 评论:0 基于 Android NDK 的学习之旅-----数据传输二(引用数据类型)(附源码) duicky

quick-cocos2d-x 学习系列之十五 状态机

quick-cocos2d-x 学习系列之十五 状态机 1.  代码 -- create Finite StateMachine self.fsm_ = {} cc.GameObject.extend(self.fsm_) :addComponent("components.behavior.StateMachine") :exportMethods() self.fsm_:setupState({ events = { {name = "start", from