zoukankan      html  css  js  c++  java
  • 【Android】7.6 FrameLayout(帧布局)

    分类:C#、Android、VS2015;

    创建日期:2016-02-11

    一、简介

    帧布局是显示多个层次图的有效手段。比如第3章介绍的百度地图就是利用帧布局来实现的,它将图层分为22级分别缓存到指定的文件夹下,程序根据用户对地图的放大程度,自动判断应该将哪一级作为最顶层的图层。另外,地图覆盖物功能也是利用它来实现的。

    帧布局的特点是:所有元素都会从容器的左上角开始放置,第一个添加的元素被放在最底层,最后一个添加的元素放在在最顶层。默认情况下,上一层的元素会覆盖下一层的元素,除非不同层元素的大小不一样(其效果是部分覆盖),或者顶层的元素是透明的。

    具体来说,对于不透明度为1(范围0.0~1.0,1为完全完全不透明)的元素,可进一步细分为:

    (1)如果下层元素和上层元素的宽高都相同,则上层元素将完全覆盖下层元素。

    (2)如果下层元素大,上层元素小,则上层元素仅部分覆盖下层元素。

    二、示例—Demo05FrameLayout

    本示例简单演示3个图层的情况:

    第0层:img1.jpg

    第1层:img2.jpg

    第2层:img3.jpg

    每单击一次【下一层】按钮,都会将图层减1,然后显示对应层的图。当减到第0层时,变为第2层,依次循环。

    注意:该示例仅为了演示图层覆盖的情况(默认都可见,但仅顶层可看到),例子并没有处理如何缓存图片的问题。

    1、运行截图

    image

    2、添加Demo05FrameLayout.axml文件

    在Resources/layout文件夹下添加该文件。

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ImageView
            android:id="@+id/image1"
            android:src="@drawable/sample_2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
        <ImageView
            android:id="@+id/image2"
            android:src="@drawable/sample_4"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
        <ImageView
            android:id="@+id/image3"
            android:src="@drawable/sample_6"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
        <Button
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一层"
            android:layout_gravity="center_horizontal" />
    </FrameLayout>

    3、添加Demo05FrameLayout.cs文件

    在SrcDemos文件夹下添加该文件。

    using System.Collections.Generic;
    using Android.App;
    using Android.OS;
    using Android.Views;
    using Android.Widget;
    namespace ch07demos.SrcDemos
    {
        [Activity(Label = "Demo05FrameLayout")]
        public class Demo05FrameLayout : Activity
        {
            List<ImageView> images = new List<ImageView>();
            int current;
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.Demo05FrameLayout);
    
                images.Add(FindViewById<ImageView>(Resource.Id.image1));
                images.Add(FindViewById<ImageView>(Resource.Id.image2));
                images.Add(FindViewById<ImageView>(Resource.Id.image3));
                current = images.Count - 1;
                FindViewById<Button>(Resource.Id.btnNext).Click +=delegate
                {
                    images[current].Visibility = ViewStates.Invisible;
                    current--;
                    if (current < 0) current = images.Count - 1;
                    images[current].Visibility = ViewStates.Visible;
                };
            }
        }
    }
  • 相关阅读:
    夺冠概率蓝桥杯
    Memcache配置
    [置顶] 6个月:从小白到程序员,也许没你想的那么难
    Delphi Messagebox自动关闭
    最近读园内的几篇好的文章的摘录及感悟
    近期项目的两点教训
    win7网络连接优先顺序设置方法
    WebSerivce学习笔记
    Delphi 控制摄像头操作
    郁闷的一天
  • 原文地址:https://www.cnblogs.com/rainmj/p/5186449.html
Copyright © 2011-2022 走看看