android 动态创建View
根据需求,有的时候需要进行动态创建布局,也就是xml中的布局需要通过addView()的形式动态的进行创建。如下图:
图中红色部分:如果在xml中,应该是这样的
<LinearLayout
android:id="@+id/ll_facility_info_create"android:layout_width="match_parent"android:padding="20dp"android:orientation="vertical"android:background="@color/calendar_color_white"android:layout_height="wrap_content"><TextView
android:layout_width="match_parent"android:layout_height="40dp"android:text="设施服务"android:textColor="#000000"android:gravity="center_vertical"android:textSize="@dimen/font20"/><!--动态创建--><LinearLayout
android:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"><TextView
android:layout_width="match_parent"android:layout_height="30dp"android:text="网络"android:textColor="#000000"android:gravity="center_vertical"/><LinearLayout
android:layout_width="match_parent"android:orientation="horizontal"android:layout_height="30dp"><TextView
android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center_vertical"android:text="wifi免费"/><TextView
android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center_vertical"android:text="wifi免费"/><TextView
android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center_vertical"android:text="wifi免费"/></LinearLayout><LinearLayout
android:layout_width="match_parent"android:orientation="horizontal"android:layout_height="30dp"><TextView
android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center_vertical"android:text="wifi免费"/><TextView
android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:visibility="invisible"android:gravity="center_vertical"android:text="wifi免费"/><TextView
android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:visibility="invisible"android:gravity="center_vertical"android:text="wifi免费"/></LinearLayout></LinearLayout></LinearLayout>
而java代码:
// 动态添加设施服务private View addFacilityLayout(){
// 初始化布局参数LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
// 创建最外层的LinearLayoutLinearLayout linearLayout = new LinearLayout(this);
// 设置布局参数linearLayout.setLayoutParams(layoutParams);
// 设置子View的LinearlayoutlinearLayout.setOrientation(LinearLayout.VERTICAL);// 第一个子View TextView
// 设置子View的布局LinearLayout.LayoutParams viewLayoutParams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,DensityUtils.dip2px(context,30));
// 创建子View TextViewTextView textView = new TextView(this);
// 设置内容的位置textView.setGravity(Gravity.CENTER_VERTICAL);
// 设置字体颜色textView.setTextColor(Color.BLACK);textView.setText("网络");
// 设置布局参数textView.setLayoutParams(viewLayoutParams1);
// 将第一子View添加到最外层的linearlayout中linearLayout.addView(textView);for (int i = 0; i < 2; i++) {// 第二个子View LinearLayoutLinearLayout.LayoutParams viewLayoutParams2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,DensityUtils.dip2px(context,30));LinearLayout linearLayout1 = new LinearLayout(this);linearLayout1.setLayoutParams(viewLayoutParams2);
// 设置子View的LinearlayoutlinearLayout1.setOrientation(LinearLayout.HORIZONTAL);// 第二个子View LinearLayout中的TextViewfor (int j = 0; j < 3; j++) {
// 设置子View的布局LinearLayout.LayoutParams vp1 = new LinearLayout.LayoutParams(0,DensityUtils.dip2px(context,30),1.0f);
// 创建子View TextViewTextView tv1 = new TextView(this);
// 设置内容的位置tv1.setGravity(Gravity.CENTER_VERTICAL);
// 设置布局参数tv1.setLayoutParams(vp1);// 设置字体颜色tv1.setText("wifi网络");
// 添加子viewlinearLayout1.addView(tv1);}
// 添加子ViewlinearLayout.addView(linearLayout1);}return linearLayout;}
调用:
LinearLayout ll_facility_info_create = findViewById(R.id.ll_facility_info_create);ll_facility_info_create.addView(addFacilityLayout());
android 动态创建View相关推荐
- android 动态创建view,react-native动态创建Android View 无效果
问题描述 react-native动态创建Android View 无效果,我想在react-native里面直接点击函数进行创建,也就是通过module中的方法创建View 问题出现的环境背景及自己 ...
- Android动态创建View
代码 View view = new View(context);LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Vi ...
- Android 动态创建控件并设置控件的大小之Android屏幕适配攻略(五)
Android 屏幕适配攻略(五)动态创建控件并设置控件的大小 题记 -- 执剑天涯,从你的点滴积累开始,所及之处,必精益求精,即是折腾每一天. 重要消息 flutter中网络请求dio使用分析 视频 ...
- android 动态添加快捷方式,Android动态创建快捷方式
一. 摘要 以Android O为分界,介绍两种动态创建快捷方式的途径:广播和ShortcutManager. 二. Android O以前 在Android O(8.0)以前,动态创建快捷方式是通过 ...
- Android动态创建快捷方式
一. 摘要 以Android O为分界,介绍两种动态创建快捷方式的途径:广播和ShortcutManager. 二. Android O以前 在Android O(8.0)以前,动态创建快捷方式是通过 ...
- Android——动态设置view背景颜色setBackgroundColor
setBackgroundColor 用于动态设置view背景颜色 今天写代码的时候遇到个小问题 动态设置的时候用Color.GRAY,系统自己带着的颜色,是没有问题的 但是用自己定义的颜色,就不生效 ...
- android 动态设置View的高度和宽度,ViewTreeObserver使用
private int mMonitorHeight = 0; private int mMonitorWidth = 0; private boolean bisSetScreen = false; ...
- android动态创建arraylist,Android:二维ArrayList帮助
目前,我已经将我的代码将用户输入放入一维ArrayList中,但是我想将它们输入到二维ArrayList中,并且遇到了一些麻烦. 这是我的代码: public class Game extends A ...
- android 动态换肤框架,GitHub - ss520k/Android-Skin-Loader: 一个通过动态加载本地皮肤包进行换肤的皮肤框架...
Android-Skin-Loader 更新日志 导入到Android Studio,使用gradle构建皮肤包(见7. 皮肤包是什么?如何生成?)(2015-12-02) 解决Fragment换肤在 ...
最新文章
- java网络流传输,中文乱码问题。
- 就国内某个程序员问答网站的简单的分析
- MaskRCNN要点
- 高性能ORM 框架之 MySqlSugar
- 关于JSF Converter转换器的知识点
- Linux学习整理-网络防火墙iptables-实践篇2
- [GBA ROM列表]不断补完中……
- cass等距离等分线段的命令键_CAD等分线段指令是什么?
- iptables和防火墙_iptables的防火墙正常运行时间和安全性
- php微信登陆流程,thinkphp微信授权登陆具体流程~
- 2021-04-12——新特性Lambda表达式和Function函数式接口编程
- SecureCRT 水平显示不全解决办法
- 唯有卡拉特拉瓦,才敢让建筑飞起来!
- CPS攻击案例(一)——基于脉冲宽度调制PWM的无人机攻击
- Kvaser Memorator Professional五通道CAN/CANFD总线分析记录仪
- 洛谷:坚果保龄球(P1413,贪心)
- AcWing 844. 走迷宫(迷宫问题+最短路径+BFS+最短路径输出)
- OpenCV图形图像击中击不中HITMISS变换处理基础知识
- 精易编程助手 v1.09官方版
- 如何在项目中引入字体库(如阿里巴巴普惠体)