基于Android在布局中动态添加view的两种方法(总结)

 更新时间:2017年10月09日 11:09:53   作者:Xd_Yu  
下面小编就为大家带来一篇基于Android在布局中动态添加view的两种方法(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一、说明

添加视图文件的时候有两种方式: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)

<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 = "动态添加布局";
 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 动态添加布局(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); 
 } 


 @Override
 public void onClick(View v) {
  // TODO 控件单击事件
  switch (v.getId()) {
  case R.id.sbtn_navback:
   this.finish();
   break;
  default:
   break;
  }
 }

}

以上这篇基于Android在布局中动态添加view的两种方法(总结)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • android教程之使用asynctask在后台运行耗时任务

    android教程之使用asynctask在后台运行耗时任务

    AsyncTask用在需要在ui线程中调用、在背景线程中执行耗时任务、并且在ui线程中返回结果的场合。下面就是一个在背景中运行的AsyncTask的实现DownloadDBTask
    2014-02-02
  • Android Camera是否支持变焦的判断方法总结

    Android Camera是否支持变焦的判断方法总结

    这篇文章主要介绍了Android Camera是否支持变焦的判断方法总结,本文总结了调节摄像头焦距编程中遇到的一些问题和解决方法,需要的朋友可以参考下
    2015-04-04
  • Android中fragment嵌套fragment问题解决方法

    Android中fragment嵌套fragment问题解决方法

    这篇文章主要介绍了Android中fragment嵌套fragment问题解决方法,本文给出两个解决方法,需要的朋友可以参考下
    2015-06-06
  • Android 驱动编写LED-NDK程序

    Android 驱动编写LED-NDK程序

    这篇文章主要介绍了Android 驱动编写LED-NDK程序的相关资料,需要的朋友可以参考下
    2016-09-09
  • Android Vitamio和ExoPlayer两种播放器优劣分析

    Android Vitamio和ExoPlayer两种播放器优劣分析

    Vitamio和ExoPlayer都是用于安卓平台的视频播放器库,它们各有优缺点,具体使用哪一个,需要根据你的实际需求、开发经验、项目规模等多个因素综合考虑
    2023-04-04
  • android View 绘制完成监听的实现方法

    android View 绘制完成监听的实现方法

    今天小编就为大家分享一篇android View 绘制完成监听的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • Android实现美团、大众点评的购买悬浮效果(ScrollView滚动监听)

    Android实现美团、大众点评的购买悬浮效果(ScrollView滚动监听)

    这篇文章主要为大家详细介绍了Android对ScrollView滚动监听,实现美团、大众点评的购买悬浮效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • Android自定义柱状图表的方法实例

    Android自定义柱状图表的方法实例

    柱状图是我们在日常开发中经常会用到的一种图表,下面这篇文章主要给大家介绍了关于Android如何自定义柱状图表的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧
    2018-05-05
  • android notification 的总结分析

    android notification 的总结分析

    notification是一种出现在任务栏的提示,特别是在4.0以后notification改进了不少,本文内容都是基于4.0及4.1以后总结来的
    2013-05-05
  • Android应用退出登录的实现方法

    Android应用退出登录的实现方法

    每一个app都会有一个”退出登陆”的功能,当点击退出之后需要将所有的Activity都finish掉,开始是想将栈中的所有Activity清除掉,但是没有找到方法,后来用广播实现了。下面小编给大家分享android应用退出登录的实现方法,需要的朋友参考下
    2017-04-04

最新评论