layout-代码中添加view
今天需要在代码中动态的给一个布局添加一个imageview,现在把方法记录如下。直接看demo代码
//创建容器
final LinearLayout layout = new LinearLayout(this);
//设置背景
layout.setBackgroundColor(0x7700ffff);
//设置垂直排列
layout.setOrientation(LinearLayout.VERTICAL);
//设置控件水平居中
layout.setGravity(Gravity.CENTER_HORIZONTAL);
//创建属性
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300,300);
addContentView(layout, layoutParams); //创建控件image
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xffff0000);
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(100,100); //设置子控件的margin数值
layoutParams2.leftMargin=50;
layoutParams2.topMargin=50;
//添加到容器
layout.addView(imageView, layoutParams2); //创建textiew
TextView textView = new TextView(this);
textView.setText("textview");
LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//设置相对父容器的位置
//layoutParams.gravity = Gravity.END;
//设置自身内容相对自身位置
//button.setGravity(Gravity.END);
//设置padding
layoutParams3.leftMargin=70;
layoutParams3.topMargin=20;
layout.addView(textView, layoutParams3);
看效果图

其实,代码中动态添加view,就跟使用xml一样的,xml中有的属性,都可以通过代码设置实现。这些属性,要么通过创建的控件(imageview)方法来设,要么就是通过
LayoutParams
其实,在这个类里面主要含有的是margin相关属性,下面是它源码的一部分
this.leftMargin = source.leftMargin;
this.topMargin = source.topMargin;
this.rightMargin = source.rightMargin;
this.bottomMargin = source.bottomMargin;
this.startMargin = source.startMargin;
this.endMargin = source.endMargin;
还有另外一个常用的relativelayout,我们也写一个小的demo看下
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/aaa"
android:layout_height="match_parent"
tools:context="com.example.testcode.MainActivity" > <TextView
android:id="@+id/tv"
android:text="origin text"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#ff0000"
/> </RelativeLayout>
代码如下
package com.example.testcode;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView; public class MainActivity extends Activity {
private TextView textView;
private RelativeLayout aaaLayout; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tv);
aaaLayout = (RelativeLayout) findViewById(R.id.aaa);
Button button = new Button(this);
RelativeLayout.LayoutParams layoutParams_button = new RelativeLayout.LayoutParams(
100, 100);
RelativeLayout relativeLayout = new RelativeLayout(this);
//LinearLayout这个方法有三个参数的,加了一个权重在里面,RelativeLayout就不存在了
//LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height, weight);
RelativeLayout.LayoutParams layoutParams_text = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//设置控件相对R.id.tv位置
//layoutParams_text.addRule(RelativeLayout.BELOW, R.id.tv);
//设置控件相对父容器位置
layoutParams_text.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParams_text.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
//layoutParams_text.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.addView(button, layoutParams_button);
aaaLayout.addView(relativeLayout, layoutParams_text);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我们看下效果

layout-代码中添加view的更多相关文章
- 代码中添加事务控制 VS(数据库存储过程+事务) 保证数据的完整性与一致性
做人事档案的系统考虑到数据的安全性与一致性,毕竟是要对外上线.真正投入使用的项目,数据库的可靠性与安全性上我们开发人员要考虑的就很多了,记得做机房收费系统时注册新卡是自己为了简单,写成了一个存储过程( ...
- Ado.Net实体数据模型EF,如何在代码中添加数据库连接密码
在创建EF模型的时候,VS2013提示说“在连接字符串中存储敏感数据可能有安全风险”,于是我选择了在代码中添加,可是如何通过代码添加呢? 我在网上百度了下,没有人说的清楚直观. 假设我们创建了一个名字 ...
- Android中如何在代码中设置View的宽和高?
Android中如何在代码中设置View的宽和高?https://zhidao.baidu.com/question/536302117.htmlhttps://blog.csdn.net/u0141 ...
- 移动端网站如何开发(电脑端网站到手机端网站我们需要在html代码中添加哪个meta标签)
移动端网站如何开发(电脑端网站到手机端网站我们需要在html代码中添加哪个meta标签) 一.总结 一句话总结: 添加viewport标签:meta name="viewport" ...
- delphi 在代码中 添加 TO-DO 并且 管理
TO-DO List是一项非常好用的功能.采用她可以让我们很清楚的了解以前完成了那些任务,还有哪些任务需要做,由谁负责完成,是不是比较紧急的任务等.今天来不及完成的,明天上班就可以很快的找到任务所在的 ...
- 在代码中写view 的长宽高等
获得资源的id的另一种方法 int layoutRes = getResources().getIdentifier("pager_view" + i, "layout& ...
- (转)Unity3d使用心得(1):ModelImporter的使用、在代码中添加动画片段。
在使用 Unity3d 倒入Fbx模型的时候,动画的动画片段需要自己手动添加模型多了以后会是一个不小的工作量. Unity3d支持 编辑器脚本来控制资源导入的过程.添加一个 AssetPostproc ...
- 如何给DropDownList在后台代码中添加一个空的选项
代码如何: ddl_dept.Items.Insert(, new ListItem("---请选择---","")); new ListItem的第一个参数表 ...
- 往Layout中动态添加View
需要注意几个方法:基本上所有的方法参数单位是px 1.设置View的宽高: LinearLayout.LayoutParams params = new LinearLayout().LayoutPa ...
随机推荐
- hive安装用mysql作为元数据库,mysql的设置
mysql的设置 在要作为元数据库的mysql服务器上建立hive数据库: #建立数据库 create database if not exists hive; #设置远程登录的权限 GRANT AL ...
- ACM-ICPC 2016 Qingdao Preliminary Contest
A I Count Two Three I will show you the most popular board game in the Shanghai Ingress Resistance T ...
- <<大学>>原文
大学之道,在明明德,在亲民,在止于至善.知止而后有定,定而后能静,静而后能安,安而后能虑,虑而后能得.物有本末,事有终始,知所先后,则近道矣. 古之欲明明德于天下者,先治其国,欲治其国者,先齐其家:欲 ...
- UI Framework-1: Aura Event Handling
Event Handling A diagram of the architecture of this system: HWNDMessageHandler owns the WNDPROC ...
- jquery validate验证规则重用
当多个控件验证规则相同时,如何避免冗余代码并应用相同规则呢? [1st way. addMethod+addClassRules] 场景:维护学生档案时需要维护父母.监护人.紧急联系人的身份证号码,此 ...
- 与 JSON 相关的一些操作 (项目中用到一部分,后续逐渐完善)
1.JSON 打印 console.log(JSON.stringify(data, null, 4)); 2.JSON 格式判断 var isjson = typeof(obj) == " ...
- [笔记-图论]Bellman-Ford
用于求可带负权的单源有向图 优化后复杂度O(nm) 如果图中存在负环,就不存在最小路 这种情况下,就一定会有一个顶点被松弛多于n-1次,Bellman-Ford可直接判断出来 我在网上看到SPFA,发 ...
- nginx配置aliyun https
server { listen 443; server_name www.goforit.com goforit.com; ssl on; ssl_certificate cert/goforit.p ...
- CMSIS-RTOS的使用
CMSIS-RTOS实现通常作为库提供.要将RTOS功能添加到现有的基于CMSIS的应用程序,需要添加RTOS库(通常是配置文件).RTOS库的可用功能在头文件cmsis_os.h中定义,该文件特定于 ...
- FastDFS学习总结(2)--Tracker与Storage配置详解
1.Tracker基本配置 # is this config file disabled # false for enabled # true for disabled disabled=false ...