Android中多層動態嵌套布局的實現

來源:互聯網
上載者:User

  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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.