---
title: "如何通过代码更改ANDROID的UI布局"
author: "leng_cn"
date: "2014-07-09T17:59:32Z"
tags: []
categories: ["编程语言"]
slug: "android-ui"
draft: false
lastmod: "2014-07-09T17:59:32Z"
id: 2090077
url: "https://leng.iteye.com/blog/2090077"
---

有如下一个XML的页面布局（略去一些东东）
 <RelativeLayout
            android:id="@+id/1"

 <RelativeLayout
            android:id="@+id/2"
<RelativeLayout
            android:id="@+id/3"
            android:layout_below="@+id/1"
当需要动态改为3的BELOW(android:layout_below="@+id/1")时，可通过如下方法变更：
lay =  this.findViewById(R.id.3);

**RelativeLayout.LayoutParams layoutParams = (android.widget.RelativeLayout.LayoutParams) lay.getLayoutParams();**

layoutParams.addRule(RelativeLayout.BELOW,2);  
lay .setLayoutParams(layoutParams);

加粗部分就是取得原有的布局参数，然后再变更，通过此方法，可以变更很多原有的布局参数