Android Layout之一:FrameLayout

Android Layout之一:FrameLayout

Android Layout有五大布局对象,分别是FrameLayout(帧布局),LinearLayout (线性布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局).

FrameLayout:该布局container可以用来占有屏幕的某块区域来显示单一的对象,可以包含有多个widgets或者是container,但是所有被包含的widgets或者是container必须被固定到屏幕的左上角,并且一层覆盖一层,不能通过为一个widgets或者是container指定一个位置。Container所包含的widgets或者是container的队列是采用的堆栈的结构,最后加进来的widgets或者是container显示在最上面。所以后一个widgets或者是container将会直接覆盖在前一个widgets或者是container之上,把它们部份或全部挡住(除非后一个widgets或者是container是透明的,必须得到FrameLayout Container的允许)。

使用模式一、

其中Main.xml 代码如下:

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

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView 

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/hello"

    />

    <TextView

        android:textColor = "#0000FF"

        android:layout_width = "wrap_content"

        android:layout_height = "wrap_content"

        android:text = "@string/hello2"

       />

</FrameLayout>

Strings.xml代码如下:

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

<resources>

    <string name="hello">Hello World, LayoutTestActivity!</string>

    <string name="app_name">LayoutTest</string>

    <string name="hello2">Hello World, I can not be LOST!</string>

</resources>

其中TextView Hello2是最后FrameLayout最后加入的,所以该TextView应该是覆盖整个屏幕的,TextView hello1被其覆盖

使用模式二、

在android中,也有类似分页标签(tab)的布局和组件。可以实现类似的功能。

看另外一个分页标签内容:

实现功能,继承TabActivity不是必须的,可以直接继承Activity编写Activity即可,见:

package com.easymorse;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;

public class TabDemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.setTitle(“演示标签分页”);
        TabHost tabHost=(TabHost) this.findViewById(R.id.tabhost);
        tabHost.setup();
        TabHost.TabSpec spec=tabHost.newTabSpec(“clockTab”);
        spec.setContent(R.id.AnalogClock01);
        spec.setIndicator(“模拟时钟”);
        tabHost.addTab(spec);
        spec=tabHost.newTabSpec(“buttonTab”);
        spec.setContent(R.id.DigitalClock01);
        spec.setIndicator(“数字时钟”);
        tabHost.addTab(spec);
        tabHost.setCurrentTab(0);
    }
}

对应的布局文件layout/main.xml:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    android:orientation=”vertical” android:layout_width=”fill_parent”
    android:layout_height=”fill_parent”>

    <TabHost android:id=”@+id/tabhost” android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”>
        <TabWidget android:id=”@android:id/tabs”
            android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
        <FrameLayout android:id=”@android:id/tabcontent”
            android:paddingTop=”80px” android:layout_width=”fill_parent”
            android:layout_height=”fill_parent”>
            <AnalogClock android:id=”@+id/AnalogClock01″
                android:layout_width=”wrap_content” android:layout_height=”wrap_content”></AnalogClock>
            <DigitalClock android:id=”@+id/DigitalClock01″
                android:layout_width=”fill_parent” android:layout_height=”fill_parent”
                android:layout_centerHorizontal=”true”></DigitalClock>
        </FrameLayout>
    </TabHost>
</LinearLayout>

这里需要注意以下几点:

  • TabHost是分页标签的总的容器,必须指定android:id为@android:id/tabhost
  • TabWidget放在TabHost容器中,必须指定android:id属性为@android:id/tabs
  • FrameLayout是Tab内容的容器,其中的每个内容都会被实现为一个分页标签,布局上要做处理,比如paddingTop,定位标签内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值