// 相对于给定ID控件
代码如下 | 复制代码 |
android:layout_above 将该控件的底部置于给定ID的控件之上; android:layout_below 将该控件的底部置于给定ID的控件之下; android:layout_toLeftOf 将该控件的右边缘与给定ID的控件左边缘对齐; android:layout_toRightOf 将该控件的左边缘与给定ID的控件右边缘对齐; android:layout_alignBaseline 将该控件的baseline与给定ID的baseline对齐; android:layout_alignTop 将该控件的顶部边缘与给定ID的顶部边缘对齐; android:layout_alignBottom 将该控件的底部边缘与给定ID的底部边缘对齐; android:layout_alignLeft 将该控件的左边缘与给定ID的左边缘对齐; android:layout_alignRight 将该控件的右边缘与给定ID的右边缘对齐; // 相对于父组件 android:layout_alignParentTop 如果为true,将该控件的顶部与其父控件的顶部对齐; android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐; android:layout_alignParentLeft 如果为true,将该控件的左部与其父控件的左部对齐; android:layout_alignParentRight 如果为true,将该控件的右部与其父控件的右部对齐; // 居中 android:layout_centerHorizontal 如果为true,将该控件的置于水平居中; android:layout_centerVertical 如果为true,将该控件的置于垂直居中; android:layout_centerInParent 如果为true,将该控件的置于父控件的中央; // 外部间隔 android:layout_marginTop 上偏移的值; android:layout_marginBottom 下偏移的值; android:layout_marginLeft 左偏移的值; android:layout_marginRight 右偏移的值; |
RelativeLayout代码
代码如下 | 复制代码 |
RelativeLayout relativeLayout = new RelativeLayout(context); Spinner spinner = new Spinner(context); edit_text_add_country = new AutoCompleteTextView(context); edit_text_add_stock = new EditText(this); RadioGroup containCountry = new RadioGroup(context); spinner.setId(1); edit_text_add_country.setId(2); edit_text_add_stock.setId(3); containCountry.setId(4); String[] mCountries = { "China" , "USA", "China2", "Comunicate"}; List<String> allcountries = new ArrayList<String>(); ArrayAdapter<String> aspnCountries; for (int i = 0; i < mCountries.length; i++) { allcountries.add(mCountries[i]); } aspnCountries = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, allcountries); aspnCountries .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(aspnCountries); edit_text_add_country.setAdapter(aspnCountries); edit_text_add_country.setHint("input country name"); edit_text_add_stock.setHint("input stock name"); RelativeLayout.LayoutParams param1, param2, param3; param1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); param2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); param2.addRule(RelativeLayout.BELOW, 1); param3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); param3.addRule(RelativeLayout.BELOW, 4); ViewGroup.LayoutParams param4, param5; param4 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); param5 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); containCountry.addView(spinner,param4); containCountry.addView(edit_text_add_country, param5); relativeLayout.addView(containCountry, param1); relativeLayout.addView(edit_text_add_stock, param3); builder.setView(relativeLayout); |
看个实例
RelativeLayout代码实现及点击设置
代码如下 | 复制代码 |
public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList<Person> personList = new ArrayList<Person>(); personList.add(new Person("zwx","23")); personList.add(new Person("zwx1","24")); personList.add(new Person("zwx2","25")); LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout01); for(Person person : personList){ TextView tv1 = new TextView(this); ll.addView(rl); |
注意:不能在RelativeLayout容器本身和他的子元素之间产生循环依赖,比如说,不能将RelativeLayout的高设置成为WRAP_CONTENT的时候将子元素的高设置成为ALIGN_PARENT_BOTTOM。