Android在布局中動態添加view的兩種方法_第1頁
Android在布局中動態添加view的兩種方法_第2頁
Android在布局中動態添加view的兩種方法_第3頁
Android在布局中動態添加view的兩種方法_第4頁
Android在布局中動態添加view的兩種方法_第5頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

Android在布局中動態添加view的兩種方法一、說明添加視圖文件的時候有兩種方式:1、通過在xml文件定義layout;2、java代碼編寫二、前言說明.構造xml文件.Layoutlnflater提到addview,首先要了解一下LayoutInflater類。這個類最主要的功能就是實現將xml表述的layout轉化為View的功能。為了便于理解,我們可以將它與findViewByld()作一比較,二者都是實例化某一對象,不同的是findViewByldO是找xml布局文件下的具體widget控件實例化,而LayoutInflater找res/layout/下的xml布局文件來實例化的。⑴創建Layoutlnflaterinflater=(Layoutlnflater)context.getSystemService(Context.LAYOUT_INFLATER_SERV1CE);或Layoutlnflaterinflater=Layoutlnflater.from(Activity,this)Layoutlnflaterinflater=getLayoutlnflater();這三種方法本質是相同的。2)inflate()用Layoutlnflater.inflate()將LayOut文件轉化成View。Viewview=inflater.inflate(R.layout,blockgymalbumlistitem,null);.添加視圖文件三、步驟1、通過在xml文件定義layout(blockgymalbum_listitem,xml)1<!--?xmlversion="1.0"encoding="utf-8"?--><linearlayoutxmlns:android="/apk/res/android"xmlns:core="/apk/res/com.gxtag.gym"android:layout_width="fill_parentnandroid:layout_height="fill__parentMandroid:orientation="vertical"android:padding="5dp"><imageviewandroid:id="@+id/iv__head_album"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/defaulthead"></imageview></linearlayout>2、main,xml<!--?xmlversion="1.0Hencoding="utf-8"?--><linearlayoutxmlns:android="/apk/res/android"android:id="@+id/ll_parent"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical">6<includeandroid:layout_width=,,fill_parentHandroid:layout_height="wrap_content"layout="@layout/block_head_back"></includex/linearlayout>3、DynamicViewActivity.xmlpackagecom.gxtag.gym.ui;2importandroid.app.Activity;importandroid.content.Context;importandroid.os.Bundle;importandroid.view.Layoutlnflater;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.view.ViewGroup;importandroid.widget.LinearLayout;importandroid.widget.LinearLayout.LayoutParams;importandroid.widget.TextView;131-importcom.gxtag.gym.R;importcom.icq.app.widget.StatedButton;16publicclassDynamicViewActivityextendsActivityimplementsOnClickListener(privateContextmContext;21privateTextviewmTv_title;22privateStringtitle="動態添加布局”;23privateStatedButtonmSbtn_back;24privateLinearLayoutmLl_parent;2526@OverrideprotectedvoidonCreate(BundlesavedlnstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_dynamic);mContext=this;initView();mLl_parent.addView(addViewl());mLl_parent.addView(addView2());3435 )36privatevoidinitView(){38 //TODO初始化視圖mLl_parent=(LinearLayout)findViewByld(R.id.ll_parent);mTv_title=(Textview)findViewById(R.id.tv_title);mTv_title.setText(String.format(String.format(getResources().getString(R.string.title),title)));mSbtn_back=(StatedButton)findViewById(R.id.sbtn_navback);mSbtn_back.setOnClickListener(this);4546)48privateViewaddViewl(){50 //TODO動態添加布局(xml方式)LinearLayout.LayoutParamsIp=newLinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);54〃Layoutlnflater55inflater1=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_IN56FLATER_SERV工CE);57〃Layoutlnflaterinflater2=getLayoutlnflater();Layoutlnflaterinflater3=Layoutlnflater.from(mContext);Viewview=inflater3.inflate(R.layout.block_gym_album_list_item,null);view.setLayoutParams(lp);returnview;757677787980818283848586878889909192939495969798991001011021031065666566676869717273privateViewaddView2(){//TODO動態添加布局(java方式)LinearLayout.LayoutParamsIp=newLinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);LinearLayoutview=newLinearLayout(this);view.setLayoutParams(lp);〃設置布局參數view.setOrientation(LinearLayout.HORIZONTAL);//設置子View的74Linearlayout//為垂直方向布局〃定乂子View中兩個元素的布局ViewGroup.LayoutParamsvlp=newViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);ViewGroup.LayoutParamsvlp2=newViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);Textviewtvl=newTextView(this);Textviewtv2=newTextView(this);tvl.setLayoutParams(vlp);〃設置Textview的布局tv2.setLayoutParams(vlp2);tvl.setText("姓名:“);tv2.setText(“李四”);tv2.setPadding(calculateDpToPx(50),0,0,0);〃設置邊距view.addView(tvl);//^Textview添加到子View中view.addView(tv2);//^Textview添加到子View中returnview;)privateintcalculateDpToPx(intpadding__in_dp){finalflo

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論