zoukankan      html  css  js  c++  java
  • Android中多层动态嵌套布局的实现

      1.概念:在开发一些复杂界面,尤其是开发平板电脑页面时,界面布局往往比手机布局复杂很多。此时就需要用到嵌套布局。同时为了达到某种效果,需要局部的页面能够动态的变化,最典型的就是在一个页面中使用多个ViewPager。当这些ViewPager所在的页面也是动态变化的时候,就需要实现多层LinearLayout的嵌套。比如一个标签页面,头部是静态的,内容部分是动态变化的,同时每个内容中又需要动态变化,代码实现时就需要通过多次添加来实现。

      2.下面是一个简单的案例(该案例只是实现了多层嵌套的添加,如果需要实现动态的添加效果,只需通过控制条件改变每次添加的内容即可。)

      PadTestActivity.java
    package com.devin;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.widget.LinearLayout;
    
    public class PadTestActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            LayoutInflater inflater=getLayoutInflater();
            
            //Add first page
            LinearLayout myFirst = (LinearLayout) inflater.inflate(
                    R.layout.first, null).findViewById(R.id.myFirst);        
            LinearLayout layoutMain = (LinearLayout)findViewById(R.id.LayoutMain);
            layoutMain.removeAllViews();
            layoutMain.addView(myFirst);    //Show the page first
            
            //Add second page
            LinearLayout mySecond = (LinearLayout) inflater.inflate(
                    R.layout.second, null).findViewById(R.id.mySecond);    
            LinearLayout layoutFirst = (LinearLayout) findViewById(R.id.LayoutFirst);
            layoutFirst.addView(mySecond);    
            
            //Add third page
            LinearLayout myThird = (LinearLayout) inflater.inflate(
                    R.layout.third, null).findViewById(R.id.myThird);    
            LinearLayout layoutSecond = (LinearLayout) findViewById(R.id.LayoutSecond);
            layoutSecond.addView(myThird);    
        }
    }

      布局代码

      main.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" >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Main Page" />
    
        <LinearLayout
            android:id="@+id/LayoutMain"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </LinearLayout>
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Main Page" />
    
    </LinearLayout>

      first.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myFirst"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="First Page" />
    
        <LinearLayout
            android:id="@+id/LayoutFirst"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </LinearLayout>
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="First Page" />
    
    </LinearLayout>

      second.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mySecond"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Second Page" />
    
        <LinearLayout
            android:id="@+id/LayoutSecond"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </LinearLayout>
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Second Page" />
    
    </LinearLayout>

      third.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myThird"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Third Page" />
    
        <LinearLayout
            android:id="@+id/LayoutThird"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </LinearLayout>
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Third Page" />
    
    </LinearLayout>

    PS: 欢迎关注公众号"Devin说",会不定期更新Java相关技术知识。
  • 相关阅读:
    2020最新版Springcloud-Alibaba ZooKeeper篇(三)
    2020最新版SpringCloud(H版&alibaba)框架开发教程(二)
    2020最新版SpringCloud(H版&alibaba)框架开发教程(一)
    String StringBuffer StringBuilder详细分析和面试题
    算法-一个经典sql 题和一个Java算法题
    大数据开发-Flume-频繁产生小文件原因和处理
    大数据开发-Hive-常用日期函数&&日期连续题sql套路
    MapReduce怎么优雅地实现全局排序
    intelliJ IDEA 鼠标光标消失问题
    maven依赖问题的出现原因与解决方式
  • 原文地址:https://www.cnblogs.com/devinzhang/p/2459097.html
Copyright © 2011-2022 走看看