1.一些常用的公共属性介绍
2. Linear Layout 线形布局
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>
3. RelativeLayout 相对定位布局
<?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); } }
先添加参照物(ImageView),然后就可以依次添加其他组件,定义位置规则rule!位置规则是不分先后的!另外ADT插件提供的预览图,有时候是不准的,未能及时更新,所以最好还是要到模拟器上测试!