public class AndrodTActivity extends Activity {
//在RelativeLayout布局中可以设置标签的android:layout_toLeftOf,android:layout_toRightOf等属性,如何用Java代码完成这些工作。
LayoutInflater inflater;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inflater = LayoutInflater.from(this);
// 装载一个布局文件,要向这个布局中动态添加一个Button
RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.main, null);
// 装载要动态添加的按钮布局
Button button2 = (Button) inflater.inflate(R.layout.button2, null);
// 创建一个LayoutParams 对象
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// addRule方法 将按钮布局添加到button1的右边
params.addRule(RelativeLayout.RIGHT_OF, R.id.button1);
// 更新将要添加按钮的属性值
button2.setLayoutParams(params);
rl.addView(button2);
setContentView(rl);
}

}

更多相关文章

  1. Android使用代码实现RelativeLayout,LinearLayout布局
  2. Android学习之表格布局管理器
  3. 【Android动态布局】之【使用addView方法时,如何保持已有动态控件
  4. android 之 布局属性--RelativeLayout
  5. Android ImageSwitcher 实现按钮的3d旋转效果
  6. android动态给view设置可见、不可见属性
  7. Android 动态设置布局属性

随机推荐

  1. Android(安卓)Gesture 手势识别
  2. Official Note of Android(安卓)(importa
  3. Android(安卓)多线程2
  4. android蓝牙开发入门到精通3---服务端客
  5. Android生命周期
  6. android 使用md5加密
  7. Android(安卓)PinnedSectionListView异常
  8. android 存储一些app配置信息
  9. Android(安卓)发布项目到私服Nexus仓库
  10. android socket client