最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android編程動(dòng)態(tài)加載布局實(shí)例詳解【附demo源碼】

 更新時(shí)間:2016年10月25日 09:45:37   作者:Terry_龍  
這篇文章主要介紹了Android編程動(dòng)態(tài)加載布局,結(jié)合實(shí)例形式分析了Android動(dòng)態(tài)加載布局的原理、操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Android編程動(dòng)態(tài)加載布局的方法。分享給大家供大家參考,具體如下:

由于前段時(shí)間項(xiàng)目需要,需要在一個(gè)頁面上加載根據(jù)不同的按鈕加載不同的布局頁面,當(dāng)時(shí)想到用 tabhot 。不過美工提供的界面圖完全用不上tabhot ,所以想到了動(dòng)態(tài)加載的方法來解決這一需求。在這里我整理了一下,寫了一個(gè) DEMO 希望大家以后少走點(diǎn)彎路。

首先,我們先把界面的框架圖畫出來,示意圖如下:

中間白色部門是一個(gè)線性布局文件,我喜歡在畫圖的時(shí)候用不同的顏色將一塊布局標(biāo)示出來,方便查看。布局文件代碼如下:

<?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">
 <LinearLayout android:orientation="horizontal"
  android:layout_width="wrap_content" android:layout_height="wrap_content">
  <Button android:text="加載ListView" android:id="@+id/Button01"
   android:layout_width="wrap_content" android:layout_height="wrap_content">
  </Button>
  <Button android:text="加載另外一個(gè)頁面" android:id="@+id/Button02"
   android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
 </LinearLayout>
 <LinearLayout android:id="@+id/LinearLayout01" android:background="#FFFFFF"
  android:layout_width="fill_parent" android:layout_height="fill_parent"></LinearLayout>
</LinearLayout>

從上面的效果圖可以看出,那塊白色的線性布局是用來動(dòng)態(tài)加載傳進(jìn)來的布局文件。好了,我們就來做如果把布局文件動(dòng)態(tài)的加載進(jìn)來。下面我們一步一步來實(shí)現(xiàn)這個(gè)效果,首先,先把需要的 XML  勾畫出來,分為步驟如下。

新建一個(gè)布局用來存放 ListView 頁面,代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:id="@+id/layout"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android">
 <ListView android:id="@+id/ListView01" android:layout_width="wrap_content"
  android:layout_height="wrap_content"></ListView>
</LinearLayout>

新建一個(gè) ListView 每一行數(shù)據(jù)的樣式,代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android">
 <TextView android:text="@+id/TextView01" android:id="@+id/TextView01"
  android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>

新建另外一個(gè)頁面,用來區(qū)分此頁面是動(dòng)態(tài)加載的,代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:id="@+id/hellolayout"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android">
 <TextView android:text="HELLO"
  android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>

實(shí)現(xiàn)ListView 的添充數(shù)據(jù),這里不詳細(xì)介紹如何填充ListView 每行數(shù)據(jù),有不解的朋友可以查看前面ListView相關(guān)文章 ,代碼如下:

package com.terry;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class listAdapter extends BaseAdapter {
 ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
 private LayoutInflater inflater;
 public listAdapter(Context contex)
 {
  inflater=LayoutInflater.from(contex);
  HashMap<String, Object> map=new HashMap<String, Object>();
  for (int i = 0; i < 10; i++) {
   map.put("name", "例子");
   list.add(map);
  }
 }
 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return list.size();
 }
 @Override
 public Object getItem(int position) {
  // TODO Auto-generated method stub
  return list.get(position);
 }
 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return position;
 }
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  final viewHolder myHolder;
  if (convertView==null) {
   myHolder=new viewHolder();
   convertView=inflater.inflate(R.layout.list_view_row, null);
   myHolder.tv=(TextView)convertView.findViewById(R.id.TextView01);
   convertView.setTag(myHolder);
  }
  else
  {
   myHolder=(viewHolder)convertView.getTag();
  }
  myHolder.tv.setText(list.get(position).get("name").toString());
  return convertView;
 }
}

項(xiàng)目大綱如下圖:

好了,到此我們的準(zhǔn)備工作就己經(jīng)完成,接下來就是要教大家如何實(shí)現(xiàn)動(dòng)態(tài)加載上面所畫的布局頁面了,先看一下效果圖:

點(diǎn)擊第一個(gè)按鈕

點(diǎn)擊第二個(gè)按鈕

動(dòng)態(tài)加載代碼如下:

package com.terry;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class dynaActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  final LayoutInflater inflater = LayoutInflater.from(this);
  Button btn = (Button) findViewById(R.id.Button01);
  Button btn2 = (Button) findViewById(R.id.Button02);
  final LinearLayout lin = (LinearLayout) findViewById(R.id.LinearLayout01);
  btn.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    LinearLayout layout = (LinearLayout) inflater.inflate(
      R.layout.listview, null).findViewById(R.id.layout);
    ListView lv=(ListView)layout.getChildAt(0);
    lv.setAdapter(new listAdapter(dynaActivity.this));
    lin.removeAllViews();
    lin.addView(layout);
   }
  });
  btn2.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    LinearLayout layout = (LinearLayout) inflater.inflate(
      R.layout.hello, null).findViewById(R.id.hellolayout);
     TextView lv=(TextView)layout.getChildAt(0);
     lv.setTextColor(Color.RED);
    lin.removeAllViews();
    lin.addView(layout);
   }
  });
 }
}

上面通過使用LayoutInflater  每次點(diǎn)擊按鈕時(shí)候去讀取布局文件,然后找到布局文件里面的各個(gè)VIEW 操作完VIEW 后加載進(jìn)我們setContentView 方面里面的要放的布局文件里面,每次動(dòng)態(tài)加載文件必需 調(diào)用 removeAllViews方法,清除之前的加載進(jìn)來的 View 。是不是很簡(jiǎn)單?當(dāng)然動(dòng)態(tài)加載VIEW 還有許多種方法,多嘗試不同寫法??赡軙?huì)領(lǐng)會(huì)不一樣的心得,祝你早上掌握android 的開發(fā)技術(shù)。

Tip:因?yàn)槭腔赩IEW 操作,因此你可以用 Animation 的動(dòng)畫效果使其更換界面更為自然,觀賞性更強(qiáng)。

完整實(shí)例源碼點(diǎn)擊此處本站下載。

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android布局layout技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • ImageView點(diǎn)擊可變暗的實(shí)例代碼(android代碼技巧)

    ImageView點(diǎn)擊可變暗的實(shí)例代碼(android代碼技巧)

    本文給大家分享一段實(shí)例代碼給大家介紹ImageView點(diǎn)擊可變暗的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧
    2017-02-02
  • Android12?藍(lán)牙適配的實(shí)現(xiàn)步驟

    Android12?藍(lán)牙適配的實(shí)現(xiàn)步驟

    本文主要介紹了Android12?藍(lán)牙適配的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Android 3.0引入的異步加載機(jī)制Loader

    Android 3.0引入的異步加載機(jī)制Loader

    Loader裝載器從android3.0開始引進(jìn)。它使得在activity或fragment中異步加載數(shù)據(jù)變得簡(jiǎn)單。下面我們就來詳細(xì)講解下
    2017-12-12
  • Android實(shí)現(xiàn)圖片裁剪處理的操作步驟

    Android實(shí)現(xiàn)圖片裁剪處理的操作步驟

    這篇文章介紹了構(gòu)建具有圖片選擇、裁剪(含手動(dòng)縮放和旋轉(zhuǎn))及保存到自定義路徑功能的 Android 應(yīng)用 demo 的步驟,包括設(shè)置權(quán)限、創(chuàng)建布局文件、實(shí)現(xiàn)自定義視圖CustomCropImageView、更新Activity邏輯等,最終完成了具有完整裁剪功能的應(yīng)用,需要的朋友可以參考下
    2025-01-01
  • Android 詳解ThreadLocal及InheritableThreadLocal

    Android 詳解ThreadLocal及InheritableThreadLocal

    這篇文章主要介紹了Android 詳解ThreadLocal及InheritableThreadLocal的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • 淺談Android RecyclerView 間距全適配

    淺談Android RecyclerView 間距全適配

    本篇文章主要介紹了淺談Android RecyclerView 間距全適配,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • Android如何通過scheme跳轉(zhuǎn)界面

    Android如何通過scheme跳轉(zhuǎn)界面

    Android如何通過scheme跳轉(zhuǎn)界面,這篇文章就為大家介紹了Android通過scheme跳轉(zhuǎn)界面的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android編程實(shí)現(xiàn)ListView滾動(dòng)提示等待框功能示例

    Android編程實(shí)現(xiàn)ListView滾動(dòng)提示等待框功能示例

    這篇文章主要介紹了Android編程實(shí)現(xiàn)ListView滾動(dòng)提示等待框功能,結(jié)合實(shí)例形式分析了Android ListView滾動(dòng)事件相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-02-02
  • Android中Handler實(shí)現(xiàn)倒計(jì)時(shí)的兩種方式

    Android中Handler實(shí)現(xiàn)倒計(jì)時(shí)的兩種方式

    本篇文章主要介紹了Android中Handler實(shí)現(xiàn)倒計(jì)時(shí)的兩種方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • Android ActionBar制作時(shí)鐘實(shí)例解析

    Android ActionBar制作時(shí)鐘實(shí)例解析

    這篇文章主要為大家詳細(xì)介紹了Android ActionBar制作時(shí)鐘的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-05-05

最新評(píng)論

海伦市| 犍为县| 衡山县| 抚州市| 正镶白旗| 自治县| 荣成市| 灵丘县| 色达县| 长岭县| 石柱| 平武县| 孟村| 桦甸市| 江永县| 新竹县| 修水县| 墨竹工卡县| 藁城市| 宝清县| 青州市| 高淳县| 乐陵市| 龙川县| 金乡县| 勐海县| 潞西市| 封丘县| 绥阳县| 江门市| 新泰市| 海城市| 册亨县| 安吉县| 玉林市| 伊川县| 宾阳县| 罗源县| 崇义县| 井研县| 时尚|