动态添加一个视图及其布局属性(要掌握)
2017-08-16 17:40
417 查看
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.admin.myapplication30.FrameActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btn_add_frame" android:gravity="center" android:text="添加视图" android:textColor="#000000" android:textSize="17sp"/> <FrameLayout android:id="@+id/fl_frame" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:foreground="@mipmap/ic_launcher" android:foregroundGravity="top|center_horizontal"> </FrameLayout> </LinearLayout> </android.support.constraint.ConstraintLayout>
package com.admin.myapplication30; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.LinearLayout; public class FrameActivity extends AppCompatActivity implements View.OnClickListener { private FrameLayout fl_content; private int[] mColorArray = { Color.BLACK, Color.WHITE, Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE, Color.CYAN, Color.MAGENTA, Color.GRAY, Color.DKGRAY }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_frame); fl_content = (FrameLayout) findViewById(R.id.fl_frame); findViewById(R.id.btn_add_frame).setOnClickListener(this); } @Override public void onClick(View v) { if (v.getId() == R.id.btn_add_frame) { int random = (int) (Math.random()*10 % 10); View vv = new View(this); vv.setBackgroundColor(mColorArray[random]); LinearLayout.LayoutParams ll_params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, (random+1)*50); vv.setLayoutParams(ll_params); vv.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View vvv) { fl_content.removeView(vvv); return true; } }); fl_content.addView(vv); } } }
相关文章推荐
- Ruby元编程:动态添加类属性及其实际应用
- 一步步学习微软InfoPath2010和SP2010--第三章节--表单设计基础:处理InfoPath布局、控件和视图(2)--添加一个布局和表格
- Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标
- Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标
- 后台动态添加布局文件、控件与动态设置属性
- 一步步学习微软InfoPath2010和SP2010--第三章节--表单设计基础:处理InfoPath布局、控件和视图(2)--添加一个布局和表格
- android一个动态添加删除网格视图的demo,可把状态保存在数据库中
- 控件类——如何给视图添加响应事件?(有一个方法及其解析)
- 为某一个对象动态添加属性
- 动态创建相对布局和 其他控件 ,添加布局属性和相对的对齐属性
- 后台动态添加布局文件、控件与动态设置属性2
- Tabcontrol动态添加TabPage(获取或设置当前选项卡及其属性)
- 后台动态添加布局文件、控件与动态设置属性
- android布局--Android fill_parent、wrap_content和match_parent的区别 三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精
- 后台动态添加布局文件、控件与动态设置属性2
- 关于如何利用原生js动态给一个空对象添加属性以及属性值
- Runtime 创建动态一个类 添加成员变量 属性
- 动态添加 data 属性的时候 发现某一个值无法取到
- 【转】ASP.NET中服务器控件Table动态生成表格及其属性介绍
- Qt动态布局,多个窗口使用同一个widget如何实现。