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>
- 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);
- }
- }