Android在布局中動態添加view的兩種方法

來源:互聯網
上載者:User

標籤:android   布局   

一、說明

    添加視圖檔案的時候有兩種方式:1、通過在xml檔案定義layout;2、java代碼編寫

二、前言說明

1.構造xml檔案

2.LayoutInflater

提到addview,首先要瞭解一下LayoutInflater類。這個類最主要的功能就是實現將xml表述的layout轉化為View的功能。為了便於理解,我們可以將它與findViewById()作一比較,二者都是執行個體化某一對象,不同的是findViewById()是找xml布局檔案下的具體widget控制項執行個體化,而LayoutInflater找res/layout/下的xml布局檔案來執行個體化的。

(1)建立

LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);或

LayoutInflater inflater = LayoutInflater.from(Activity.this);或

LayoutInflater inflater = getLayoutInflater();

這三種方法本質是相同的。

(2)inflate()

用LayoutInflater.inflate() 將LayOut檔案轉化成VIew。

View view = inflater.inflate(R.layout.block_gym_album_list_item, null);

3.添加視圖檔案


三、步驟
    1、通過在xml檔案定義layout(block_gym_album_list_item.xml)
     
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:core="http://schemas.android.com/apk/res/com.gxtag.gym"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:padding="5dp" >    <ImageView        android:id="@+id/iv_head_album"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/defaulthead" /></LinearLayout>

2、main.xml

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

3、DynamicViewActivity.xml

package com.gxtag.gym.ui;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.LinearLayout;import android.widget.LinearLayout.LayoutParams;import android.widget.TextView;import com.gxtag.gym.R;import com.icq.app.widget.StatedButton;public class DynamicViewActivity extends Activity implements OnClickListener{private Context mContext;private TextView mTv_title;private String title = "動態添加布局";private StatedButton mSbtn_back;private LinearLayout mLl_parent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_dynamic);mContext=this;initView();mLl_parent.addView(addView1());mLl_parent.addView(addView2());}private void initView() {// TODO 初始化視圖mLl_parent=(LinearLayout) findViewById(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);}    private View addView1() {// TODO 動態添加布局(xml方式)LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(                 LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); //LayoutInflater inflater1=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//LayoutInflater inflater2 = getLayoutInflater();LayoutInflater inflater3 = LayoutInflater.from(mContext);View view = inflater3.inflate(R.layout.block_gym_album_list_item, null);view.setLayoutParams(lp);return view;}private View addView2() {// TODO 動態添加布局(java方式)LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(                 LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); LinearLayout view = new LinearLayout(this);         view.setLayoutParams(lp);//設定布局參數         view.setOrientation(LinearLayout.HORIZONTAL);// 設定子View的Linearlayout// 為垂直方向布局         //定義子View中兩個元素的布局         ViewGroup.LayoutParams vlp = new ViewGroup.LayoutParams(                 ViewGroup.LayoutParams.WRAP_CONTENT,                 ViewGroup.LayoutParams.WRAP_CONTENT);         ViewGroup.LayoutParams vlp2 = new ViewGroup.LayoutParams(                 ViewGroup.LayoutParams.WRAP_CONTENT,                 ViewGroup.LayoutParams.WRAP_CONTENT);                  TextView tv1 = new TextView(this);         TextView tv2 = new TextView(this);         tv1.setLayoutParams(vlp);//設定TextView的布局         tv2.setLayoutParams(vlp2);         tv1.setText("姓名:");         tv2.setText("李四");         tv2.setPadding(calculateDpToPx(50), 0, 0, 0);//設定邊距         view.addView(tv1);//將TextView 添加到子View 中         view.addView(tv2);//將TextView 添加到子View 中         return view; }private int calculateDpToPx(int padding_in_dp){         final float scale = getResources().getDisplayMetrics().density;         return  (int) (padding_in_dp * scale + 0.5f);     } @Overridepublic void onClick(View v) {// TODO 控制項單擊事件switch (v.getId()) {case R.id.sbtn_navback:this.finish();break;default:break;}}}

4、有需要再聯絡我:qq-508181017












聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.