基于Android在布局中動態(tài)添加view的兩種方法(總結(jié))
一、說明
添加視圖文件的時候有兩種方式:1、通過在xml文件定義layout;2、java代碼編寫
二、前言說明
1.構(gòu)造xml文件
2.LayoutInflater
提到addview,首先要了解一下LayoutInflater類。這個類最主要的功能就是實現(xiàn)將xml表述的layout轉(zhuǎn)化為View的功能。為了便于理解,我們可以將它與findViewById()作一比較,二者都是實例化某一對象,不同的是findViewById()是找xml布局文件下的具體widget控件實例化,而LayoutInflater找res/layout/下的xml布局文件來實例化的。
(1)創(chuàng)建
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);或
LayoutInflater inflater = LayoutInflater.from(Activity.this);或
LayoutInflater inflater = getLayoutInflater();
這三種方法本質(zhì)是相同的。
(2)inflate()
用LayoutInflater.inflate() 將LayOut文件轉(zhuǎn)化成VIew。
View view = inflater.inflate(R.layout.block_gym_album_list_item, null);
3.添加視圖文件
三、步驟
1、通過在xml文件定義layout(block_gym_album_list_item.xml)
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 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"> </imageview> </linearlayout>
activity_dynamic
<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"> </include> </linearlayout>
3、MainActivity
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 MainActivityextends Activity implements OnClickListener{
private Context mContext;
private TextView mTv_title;
private String title = "動態(tài)添加布局";
private StatedButton mSbtn_back;
private LinearLayout mLl_parent;
@Override
protected 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 動態(tài)添加布局(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 動態(tài)添加布局(java方式)
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout view = new LinearLayout(this);
view.setLayoutParams(lp);//設(shè)置布局參數(shù)
view.setOrientation(LinearLayout.HORIZONTAL);// 設(shè)置子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);//設(shè)置TextView的布局
tv2.setLayoutParams(vlp2);
tv1.setText("姓名:");
tv2.setText("李四");
tv2.setPadding(calculateDpToPx(50), 0, 0, 0);//設(shè)置邊距
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);
}
@Override
public void onClick(View v) {
// TODO 控件單擊事件
switch (v.getId()) {
case R.id.sbtn_navback:
this.finish();
break;
default:
break;
}
}
}
以上這篇基于Android在布局中動態(tài)添加view的兩種方法(總結(jié))就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android使用ViewPager實現(xiàn)類似laucher左右拖動效果
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實現(xiàn)類似laucher左右拖動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Android App中使用AudioManager類來編寫音頻播放器
這篇文章主要介紹了Android App中使用AudioManager類來編寫音樂播放器的方法,文中舉了一個簡單的例子實現(xiàn)了基礎(chǔ)的播放暫停和靜音等功能,需要的朋友可以參考下2016-04-04
Android中實現(xiàn)OkHttp上傳文件到服務(wù)器并帶進(jìn)度
本篇文章主要介紹了Android中實現(xiàn)OkHttp上傳文件到服務(wù)器并帶進(jìn)度,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
android使用ItemDecoration給RecyclerView 添加水印
本篇文章主要介紹了android使用ItemDecoration給RecyclerView 添加水印,介紹了自定義Drawable來完成水印圖片和使用ItemDecoration來布局水印,有興趣的可以了解一下。2017-02-02
Android的Fragment的生命周期各狀態(tài)和回調(diào)函數(shù)使用
這篇文章主要介紹了Android的Fragments的生命周期各狀態(tài)和回調(diào)函數(shù)使用,Fragments的生命周期與Activity息息相關(guān),需要的朋友可以參考下2016-02-02

