国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

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

2019-12-12 04:57:42
字體:
供稿:網(wǎng)友

本文實(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ì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 监利县| 东乌珠穆沁旗| 山阳县| 牙克石市| 莎车县| 洞头县| 阿克陶县| 江永县| 扎赉特旗| 禹州市| 枣庄市| 德安县| 翁源县| 远安县| 荣成市| 黑龙江省| 霍林郭勒市| 伊金霍洛旗| 广元市| 达拉特旗| 调兵山市| 武山县| 苍梧县| 寿阳县| 汽车| 资兴市| 托克逊县| 奉贤区| 延边| 富蕴县| 华亭县| 彭州市| 三穗县| 灵山县| 扎兰屯市| 南木林县| 永济市| 靖州| 桐梓县| 永平县| 沙雅县|