基于Android在布局中动态添加view的两种⽅法(总结)
⼀、说明
添加视图⽂件的时候有两种⽅式:1、通过在xml⽂件定义layout;2、java代码编写
⼆、前⾔说明
1.构造xml⽂件
2.LayoutInflater
提到addview,⾸先要了解⼀下LayoutInflater类。这个类最主要的功能就是实现将xml表述的layout转化为View的功能。为了便于理解,我们可以将它与findViewById()作⼀⽐较,⼆者都是实例化某⼀对象,不同的是findViewById()是xml布局⽂件下的具体widget控件实例化,⽽LayoutInflaterres/layout/下的xml布局⽂件来实例化的。
(1)创建
LayoutInflater inflater=(SystemService(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_l)
<linearlayout
xmlns:android="schemas.android/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="schemas.android/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
ag.gym.ui;
import android.app.Activity;
t.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;
ag.gym.R;
import com.icq.app.widget.StatedButton;
public class MainActivityextends Activity implements OnClickListener{
private Context mContext;
private TextView mTv_title;
private String title = "动态添加布局";
private StatedButton mSbtn_back;
private LinearLayout mLl_parent;
@Override
protected void onCreate(Bundle 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=(SystemService(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(
android layout布局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);
}
@Override
public void onClick(View v) {
// TODO 控件单击事件
switch (v.getId()) {
case R.id.sbtn_navback:
this.finish();
break;
default:
break;
}
}
}
以上这篇基于Android在布局中动态添加view的两种⽅法(总结)就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论