Android开发 --代码布局

在线性布局LinearLayout里加入view比较简单,因为属性比较少,布局简单

示例,加入一个TextView

LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
TextView tv = new TextView(this);
tv.setText("hello,world");
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.addView(tv,lp);

在相对布局中RelativeLayout中加入view,属性较多

示例,加入TextView和Button,让TextView居中,并且设置Button在TextView的下方

RelativeLayout layout;
TextView tv = new TextView(this);
tv.setText("hello,world");
Button btn = new Button(this);
btn.setText("button");
tv.setId(0x011);
btn.setId(0x012);
LayoutParams tvLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutParams btnLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//添加布局规则,居中于父类
tvLp.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);
//添加布局规则,在tv的下方
btnLp.addRule(RelativeLayout.BELOW, tv.getId());
layout.addView(tv,tvLp);
layout.addView(btn,btnLp);

public void addRule(int verb, int anchor) 方法就是给view设定布局规则,verb是规则属性,就是xml文件中的各种属性值,anchor是依靠的view的id或者比如上面的RelativeLayout.CENTER_IN_PARENT的时候就是设置true或false


package com.example.android_activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//动态加载布局文件
setContentView(R.layout.activity_main);
//使用代码动态为当前界面添加一个Button
Button button=new Button(this);
button.setText("代码布局");
//设置位置
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
     //给按钮设置单击事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"代码布局",Toast.LENGTH_LONG).show();
}
});
//获取activity_main容器,将上面的按钮加入到该容器中
LinearLayout root=(LinearLayout) findViewById(R.id.activity_main);
root.addView(button);
}
}

最新文章

  1. 浅尝ECMAScript6
  2. LINK1123:failure during conversion to COFF:file invalid or corrupt
  3. adb 命令集合
  4. Gmail邮箱添加域名解析
  5. JavaScript之动态背景登陆表单
  6. oracle 直接客户端使用
  7. tableView创建方法调用的研究
  8. cf466B Wonder Room
  9. HTML代码中<%%>、<%=%>、<%:%>各是什么意思?分别用来实现什么的?
  10. java用正则表达式获取domain
  11. CODEFORCES-PROBLEMSET
  12. 28.C++- 单例类模板(详解)
  13. 第五章:shiro密码加密
  14. slf4j+log4j2模式的日志搭建
  15. OO第一单元总结(表达式求导)
  16. springboot使用@data注解,减少不必要代码
  17. SmartGit 常见错误提示代码
  18. 【leetcode-89 动态规划】 格雷编码
  19. 《Inside C#》笔记(十五) 非托管代码 下
  20. d3 + geojson in node

热门文章

  1. English trip Spoken English & Word List(updating...)
  2. Hibernate第一个程序(最基础的增删改查) --Hibernate
  3. P3489 付公主的背包
  4. BGP华为、思科选路规则
  5. Known Notation ZOJ - 3829 (后缀表达式,贪心)
  6. 第三周学习进度条+PSP0过程文档
  7. 【JS】【4】字符串数字比较大小
  8. leetcode-algorithms-26 Remove Duplicates from Sorted Array
  9. awr相关
  10. 【转】js生成接口请求参数签名加密