广告
返回顶部
首页 > 资讯 > 精选 >基于Android在布局中动态添加view的两种方法(总结)
  • 398
分享到

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

android动态添加view 2023-05-30 20:05:03 398人浏览 安东尼
摘要

一、说明 添加视图文件的时候有两种方式:1、通过在xml文件定义layout;2、java代码编写二、前言说明1.构造xml文件2.LayoutInflater提到addview,首先要了解一下LayoutInflater类。这个类最主要的

一、说明

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

--结束END--

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

本文链接: https://www.lsjlt.com/news/220831.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作