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 ...
随机推荐
- 【基础篇】Android中获取Drawable的方法
public static Drawable getDrawable(Context context,String filename) { BitmapDrawable drawable=null; ...
- 使用google API之前需要對input 做什麼 安全性的處理?
我正要使用node.js 和 google map api做一个小应用,Google MAP API的使用URL如下: https://maps.googleapis.com/maps/api/pla ...
- 关于eclipse的注释和反注释的快捷键
使用eclipse那么久了额,对注释和反注释的快捷键一直很模糊,现在记下来,方便查看. 注释和反注释有两种方式.如对下面这段代码片段(①)进行注释: private String value; pri ...
- vue之filter用法
1.全局写法: 全局过滤器必须写在vue实例创建之前. Vue.filter('testfilter', function (value,text) { // 返回处理后的值 return value ...
- [NOIP2014提高组]寻找道路
题目:洛谷P2296.Vijos P1909.codevs3731.UOJ#19. 题目大意:给你一张有向图,边权为1,让你找一条s到t的最短路径,但这条路径上所有点的出边所指向的点都与终点连通.如果 ...
- 三种记录 MySQL 所执行过的 SQL 语句的方法
程式 Debug 有時後從前面第一行追起來很辛苦(程式碼太多或 compile 過), 另一種做法就是從後面追起來, 反正最後寫入的是 DB, 那就從 DB 開始往前推, 所以就是要抓程式是執行哪些 ...
- 使用glPushMatrix和glPopMatrix的原因
转自 百度百科 glPushMatrix 函数将当前矩阵堆栈推送,通过一个,复制当前矩阵. 这就是后 glPushMatrix 的调用堆栈的顶部矩阵是它下面的相同的. 1. 原理讲解 终于明白 ...
- ArcGIS Engine获得要素的中心点坐标
IPoint centerPoint =new PointClass();//获得要素的中心点 IArea pArea = pFeature.Shape as IArea; pArea.QueryCe ...
- myeclipse2013 jsp编辑初始化
首先,大家可能有过这种经历.双击打开jsp编辑.它默认会打开视图,这样就使人恼火了,卡死了.所以我们能够自己设jsp的默认打开方式:打开Window-->preferences得: 搜索edit ...
- Spring官方文档翻译——15.4 处理器映射(Handler mappings)
15.4 处理器映射(Handler mappings) 注:以下将handler翻译成处理器 在Spring早先的版本号中,用户还须要在web应用上下文中定义处理器映射来配置请求(requests) ...