xml代码:
<xml version="1.0" encoding="utf-8"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:text="@string/hello" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/edittext" /> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:text="text1" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="Button01" android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:text="text2" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="buttom" /> </LinearLayout>
<xml version="1.0" encoding="utf-8"> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/relativelayout"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" /> <TextView android:id="@+id/text1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:layout_toRightOf="@id/image" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="button1" android:layout_toRightOf="@id/image" android:layout_below="@id/text1" /> </RelativeLayout>Java代码(动态添加组件):
public class RelativeDemo extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.relative); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, //width ViewGroup.LayoutParams.WRAP_CONTENT //height ); //设置editText layout_below="@id/button1" lp.addRule(RelativeLayout.BELOW, R.id.button1); //设置editText layout_alignLeft="@id/image" lp.addRule(RelativeLayout.ALIGN_LEFT, R.id.image); ((RelativeLayout) findViewById(R.id.relativelayout)).addView( new EditText(this), lp); } }效果图:
先添加参照物(ImageView),然后就可以依次添加其他组件,定义位置规则rule!位置规则是不分先后的!另外ADT插件提供的预览图,有时候是不准的,未能及时更新,所以最好还是要到模拟器上测试!
RelativeLayout的xml属性很多,总的来说分为2类: 1)要指定参照物的,layout_alignBottom,layout_toLeftOf,layout_above,layout_alignBaseline系列的; layout_above = ”@id/text1“ 2)以parent为参照物,设置true/false,layout_centerVertical,layout_alignParentLeft系列的。 layout_alignParentLeft = ”true“ 其中layout_alignWithParentIfMissing是比较有用且要注意的属性,当设置为true,在指定的参照物找不到的情况下,会使用parent作为新的参照物! RelativeLayout.LayoutParams是用于设置位置规则的。上述的xml属性均来自此静态类。但它的AddRule(int verb, int anchor),参数的verb动作却是引用RelativeLayout的常量,而这些常量和部分xml属性相对应。参数anchor的值可能是参照物的id,RelativeLayout.TRUE,-1(当不需要指定anchor的verb)。可以这样理解verb和anchor: xml属性(verb) = "value"(anchor) 其中它的构造函数之一: RelativeLayout.LayoutParams(int w, int h),参数指定所要设置子View的宽度和高度。 0 0 标签: Android布局管理LinearLayoutRelativeLayout
热门源码