动态加载布局

大部分情况下,在Activity的onCreate()中,使用setContentView(.xml); 来加载布局文件。

但某些时刻,需要动态加载布局。举例实现如下:


实现目标:

1.主Layout Xml中包含一个Button(用来触发切换布局)和一个LinearLayout。

2.A Layout XML是个Linear,其中中包含一个Button(用来示例如何取新Layout XML中的View)和一个TextView。

3.B  Layout XML也是个Linear, 其中包括一个Button和一个TextView。

为了容易的区别他们。background分别设置为:#000000FF, #00FF00 , #FF0000.


按下主Layout中的Button。则它将A LinearLayout替换到它的Linear区域。(动态加载Layout 到局部)

再按一次主Layout中的Button, 则用B Layout替换自己。(动态加载全部)

A Layout中的Button,则切换Button Text。(动态加载的Layout的View的获取和使用)


先看三个Layout XML:


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootlinear"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonlayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NextLayout" >
    </Button>

    <LinearLayout
        android:id="@+id/baseline"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#0000FF"
        android:orientation="horizontal" >

<!--

将要被layout1.xml替换Linear

-->
    </LinearLayout>

</LinearLayout>


layout1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FF0000"
    android:orientation="vertical" >

    <Button
        android:id="@+id/layout1but1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Layout1" />

    <TextView
        android:id="@+id/layout1text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Layout1Text" />

</LinearLayout>



layout3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayout3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00FF00"
    android:orientation="vertical" >

    <Button
        android:id="@+id/layout3but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Layout3" />

    <TextView
        android:id="@+id/layout3text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Lyaout3Text" />

</LinearLayout>



先介绍实现动态Layout的关键:android.view.LayoutInflater

Inflater的含义就是充气泵。

它用来将Layout XML实例化成为一个对应的View。(不要直接构建LayoutInflater).而是从当前Context得到它。

使用getLayoutInflater() 或者LayoutInflater.form()获取它。

得到对应Context Inflater后。 可以使用其方法 inflate(int resource, ViewGroup root) 从xml文件中填充一个View。



下面是代码部分:



/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.example.spinner;

import org.w3c.dom.Text;

import com.android.example.spinner.R;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.sax.RootElement;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;




public class SpinnerActivity extends Activity {

    public Context context;

    private Button main_layout_button;

    private LinearLayout baseLinear;
    // private LinearLayout rootLayout;
    private static int index = 0;
    private View rootView;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);    //静态载入了main.xml
        
        
        
        rootView = findViewById(R.id.rootlinear);  //就是主View的最顶层ViewGroup。main.xml中,顶层ViewGroup的ID就是rootlinear. 
        main_layout_button = (Button) findViewById(R.id.buttonlayout); // 主View中的那个Button
        baseLinear = (LinearLayout) findViewById(R.id.baseline);   //主Layout中的那个将要被替换的Linear 的View

        

//填充两个View。分别和layout1.xml和layout3.xml对应。
        LayoutInflater inflater = LayoutInflater.from(this);
        final LinearLayout linearlayout1 = (LinearLayout)inflater.inflate(R.layout.layout1, null);
        final LinearLayout linearlayout2 = (LinearLayout)inflater.inflate(R.layout.layout3, null);
        
        //当点击Button时
        main_layout_button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (index == 0) {
                    
                    baseLinear.removeAllViews();
                    baseLinear.addView(linearlayout1);

    //此时Layout1 中的Button才被实例化
                    Button layout1_button = (Button) linearlayout1
                            .findViewById(R.id.layout1but1);
                    layout1_button.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            Button button = (Button) v.findViewById(R.id.layout1but1);
                            button.setText("Test");
                            
                        }
                    });

                    index++;
                }

                else if (index == 1) {
                    ((LinearLayout)rootView).removeAllViews();
                    ((LinearLayout)rootView).addView(linearlayout2);
                    index = 0;
                }

            }
        });

    }

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值