一、说明 添加视图文件的时候有两种方式: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文档到电脑,方便收藏和打印~
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0